您好,欢迎访问一九零五行业门户网

XML学习(一)元素,属性,读取详解

元素 与 属性
javascript读取xml文件:
<?xml version="1.0" encoding="iso-8859-1" ?>
<note>
<to>duncan
</to>
<from>john</from>
<heading>reminder</heading>
<body>don't forget the meeting!</body>
</note>

xml dom 操作xml
<html>
<head>
<script type="text/javascript">
function parsexml()
{
try //internet explorer
{
xmldoc=new activexobject("microsoft.xmldom");
}
catch(e)
{
try //firefox, mozilla, opera, etc.
{
xmldoc=document.implementation.createdocument("","",null);
}
catch(e)
{
alert(e.message);
return;
}
}
xmldoc.async=false;
xmldoc.load("note.xml");
document.getelementbyid("to").innerhtml=
xmldoc.getelementsbytagname("to")[0].childnodes[0].nodevalue;
document.getelementbyid("from").innerhtml=
xmldoc.getelementsbytagname("from")[0].childnodes[0].nodevalue;
document.getelementbyid("message").innerhtml=
xmldoc.getelementsbytagname("body")[0].childnodes[0].nodevalue;
}
</script>
</head>
<body onload="parsexml()">
<h1>w3school.com.cn internal note</h1>
<p><b>to:</b> <span id="to"></span><br />
<b>from:</b> <span id="from"></span><br />
<b>message:</b> <span id="message"></span>
</p>
</body>
</html>

重要注释xmldoc.getelementsbytagname("to")[0].childnodes[0].nodevalue
xmldoc -由解析器创建的 xml 文档
getelementsbytagname("to")[0] - 第一个 <to> 元素
childnodes[0] - <to> 元素的第一个子元素(文本节点)
nodevalue - 节点的值(文本本身)
疑问:
如果xml文件为:
<?xml version="1.0" encoding="iso-8859-1" ?>
<note>
<to>asdfsd
<too>duncan1</too>
</to>
<too>duncan2</too>
<from>john</from>
<heading>reminder</heading>
<body>don't forget the meeting!</body>
</note>

读取第一个<too>
xmldoc.getelementsbytagname("to")[0].getelementsbytagname("t00")[0].childnodes[0].nodevalue
读取第二个<too>
xmldoc.getelementsbytagname("too")[0].childnodes[0].nodevalue
以上就是xml学习(一)元素,属性,读取详解的详细内容。
其它类似信息

推荐信息