xml节点信息

xml: 

<?xml version="1.0" encoding="utf-8" ?>
<library>
 <name>首都图书馆</name>
 <address>朝阳区华威桥南</address>
 <books>
  <book type="math">
   <id>0000</id>
  </book>
  <book type="computer">
   <id>0001</id>
   <name>Xml初学</name>
   <publisher>人民出版社</publisher>
   <publishdate>2010-05-0-18</publishdate>
   <fee>100.54</fee>
  </book>
  <book type="computer">
   <id>0002</id>
   <name>XSD定义</name>
   <author>子弟</author>
   <publisher>子弟出版社</publisher>
   <publishdate>2010-05-0-18</publishdate>
   <fee>102.54</fee>
  </book>
 </books>
</library>

 

cs:

            XmlDocument doc=new XmlDocument();
            doc.Load(System.AppDomain.CurrentDomain.BaseDirectory+"/htmlxml/books.xml");
            XmlNodeList nodelist = doc.GetElementsByTagName("book");
            XmlElement element = doc.GetElementById("b3");
            XmlNode node = doc.SelectSingleNode("library/books/book[2]");
            XmlNodeList nodelistselect = doc.SelectNodes("library/books/book");
            Response.Write("Name: " + node.Name + "<br/>");
            Response.Write("NodeType: " + node.NodeType + "<br/>");
            Response.Write("Value:" + node.Value + "<br/>");
            Response.Write("InnerText: " + node.InnerText + "<br/>");
            Response.Write(@"InnerXml: " + node.InnerXml+"<br/>");
            Response.Write(@"OuterXml: " + node.OuterXml + "<br/>");
            Response.Write("HasChildNodes: " + node.HasChildNodes + "<br/>");
            Response.Write("---------------------------------" + "<br/>");
            Response.Write("FirstChild: " + node.FirstChild.Name + "<br/>");
            Response.Write("LastChild: " + node.LastChild.Name + "<br/>");
            Response.Write("---------------------------------" + "<br/>");
            Response.Write("Atrribute: <br/>");
            foreach (XmlAttribute atrribute in node.Attributes)
            {
                Response.Write("Name: " + atrribute.Name);
            }
            Response.Write("----------------------------------" + "<br/>");
            Response.Write("node: <br/>");
            foreach (XmlNode cnode in node.ChildNodes)
            {
                if(cnode.NodeType==XmlNodeType.Element&&cnode.FirstChild.NodeType==XmlNodeType.Text)
                    Response.Write("Name: " + cnode.Name+"<br/>");
            }

 

结果:

Name: book
NodeType: Element
Value:
InnerText: 0001Xml初学人民出版社2010-05-0-18100.54
InnerXml: 0001Xml初学人民出版社2010-05-0-18100.54
OuterXml: 0001Xml初学人民出版社2010-05-0-18100.54
HasChildNodes: True
---------------------------------
FirstChild: id
LastChild: fee
---------------------------------
Atrribute:
Name: type----------------------------------
node:
Name: id
Name: name
Name: publisher
Name: publishdate
Name: fee

 

js:

function loadXMLDoc(dname) {
            var xmlDoc;
            // code for IE
            if (window.ActiveXObject) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument) {
                xmlDoc = document.implementation.createDocument("", "", null);
            }
            else {
                alert('Your browser cannot handle this script');
            }
            xmlDoc.async = false;
            xmlDoc.load(dname);
            return (xmlDoc);
        }

        xmlDoc = loadXMLDoc("/htmlxml/books.xml");
        var y = xmlDoc.getElementsByTagName("library/books/book[2]");
        var x = y[0];
        for (var i = 0; i < x.childNodes.length; i++) {
            document.write("name: " + x.childNodes[i].tagName + "<br/>");
        }
        document.write("-------------------------------------<br/>");
        for (var j = 0; j < x.attributes.length; j++) {
            document.write("atrribute: " + x.attributes[j].name + "<br/>");
        }

 

结果:

name: id
name: name
name: author
name: publisher
name: publishdate
name: fee
-------------------------------------
atrribute: type

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值