您的位置:首页 > Web前端 > JavaScript

microsoft.xmldom(一) xml文档遍历js

2010-05-19 14:45 405 查看
xml:

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

js:

<script type="text/javascript">

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);
}
function ListXMLDoc(xmlnode) {
var i = 0;
for (; i < xmlnode.length; i++) {

//此处不可写成 for (i=0; i < xmlnode.length; i++) 这样内循环和外循环用的是一样的i

//也可以这样写 for (var i=0; i < xmlnode.length; i++)
if (xmlnode[i].nodeType == 1) {//nodeType ①
document.write(xmlnode[i].nodeName);//nodeName②
if (xmlnode[i].firstChild.nodeType == 3)
document.write(":"+xmlnode[i].xml);//或者xmlnode[i].firstChild.nodeValue//nodeValue③
document.write("<br />");

if (xmlnode[i].childNodes.length > 0&&xmlnode[i].firstChild.nodeType==1)
ListXMLDoc(xmlnode[i].childNodes);
}
}
}
xmlDoc = loadXMLDoc("/htmlxml/books.xml");
var x = xmlDoc.getElementsByTagName("library/books/book");
ListXMLDoc(x);
</script>

cs(c#):

protected void Page_Load(object sender, EventArgs e)
{
//获取当前路径
string a=System.IO.Directory.GetCurrentDirectory();
string b = System.Environment.CurrentDirectory;
string c = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string d = System.AppDomain.CurrentDomain.BaseDirectory;
string e1 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

ListXmlDoc(ReadXMLDoc(System.AppDomain.CurrentDomain.BaseDirectory+"/htmlxml/books.xml"));
}

public XmlNodeList ReadXMLDoc(string path)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
return doc.GetElementsByTagName("library/books/book");
}

public void ListXmlDoc(XmlNodeList nodelist)
{
foreach(XmlNode node in nodelist)
{
if (node.NodeType == XmlNodeType.Element)//XmlNodeType④
{
if (node.FirstChild.NodeType == XmlNodeType.Text)
Response.Write(node.Name+":" + node.FirstChild.Value + "<br/>");
else
Response.Write(node.Name + "<br/>");
}
ListXmlDoc(node.ChildNodes);
}
}

结果:

book
id:0000
book
id:0001
name:Xml初学
publisher:人民出版社
publishdate:2010-05-0-18
fee:100.54
book
id:0002
name:XSD定义
author:子弟
publisher:子弟出版社
publishdate:2010-05-0-18
fee:102.54

注解:

①nodeType(js):

元素类型-节点类型:元素-1;属性-2;文本-3;注释-8;文档-9;

nodeType 属性规定节点的类型。

nodeType 是只读的。

②nodeName(js):

④XmlNodeType(c#):

public enum XmlNodeType
{
// 摘要:
// 如果未调用 Read 方法,则由 System.Xml.XmlReader 返回。
None = 0,
//
// 摘要:
// 元素(例如,<item>)。
Element = 1,
//
// 摘要:
// 属性(例如,id='123')。
Attribute = 2,
//
// 摘要:
// 节点的文本内容。
Text = 3,
//
// 摘要:
// CDATA 节(例如,<![CDATA[my escaped text]]>)。
CDATA = 4,
//
// 摘要:
// 实体引用(例如,#)。
EntityReference = 5,
//
// 摘要:
// 实体声明(例如,<!ENTITY...>)。
Entity = 6,
//
// 摘要:
// 处理指令(例如,<?pi test?>)。
ProcessingInstruction = 7,
//
// 摘要:
// 注释(例如,<!-- my comment -->)。
Comment = 8,
//
// 摘要:
// 作为文档树的根的文档对象提供对整个 XML 文档的访问。
Document = 9,
//
// 摘要:
// 由以下标记指示的文档类型声明(例如,<!DOCTYPE...>)。
DocumentType = 10,
//
// 摘要:
// 文档片段。
DocumentFragment = 11,
//
// 摘要:
// 文档类型声明中的表示法(例如,<!NOTATION...>)。
Notation = 12,
//
// 摘要:
// 标记间的空白。
Whitespace = 13,
//
// 摘要:
// 混合内容模型中标记间的空白或 xml:space="preserve" 范围内的空白。
SignificantWhitespace = 14,
//
// 摘要:
// 末尾元素标记(例如,</item>)。
EndElement = 15,
//
// 摘要:
// 由于调用 System.Xml.XmlReader.ResolveEntity() 而使 XmlReader 到达实体替换的末尾时返回。
EndEntity = 16,
//
// 摘要:
// XML 声明(例如,<?xml version='1.0'?>)。
XmlDeclaration = 17,
}

nodeName 是只读的

元素节点的 nodeName 与标签名相同

属性节点的 nodeName 是属性的名称

文本节点的 nodeName 永远是 #text

文档节点的 nodeName 永远是 #document

③nodeValue(js):

元素的nodeValue undefine

属性的nodeValue 属性的值

文本的nodeValue 文本的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: