问两个问题,高手一定要指教:
1<items>
2<aaa a="" id="" width="">
3</aaa>
4<bbb id="" width="" z="">
5</bbb>
6<ccc id="">
7</ccc>
8</items>
问题1:
能不能用循环遍历items中的所有子元素,该如何做???
问题2:
在事先不知道元素aaa有多少属性,以及不知道属性名的情况下,把所有的属性都提取出来,该如何做???
---------------------------------------------------------------
try something like (in Javascript using MSXML):
1<script language="javascript">
2var xmldoc = new ActiveXObject("Microsoft.XMlDOM");
3xmldoc.async = false;
4xmldoc.load("items.xml");
5alert(xmldoc.xml);
6walk(xmldoc.documentElement);
7function walk(node)
8{
9alert(node.nodeName + ":" + node.text);//node.nodeValue);
10
11if (node.attributes != null && node.attributes.length > 0)
12{
13for (var i=0; i<node.attributes.length; i++)
14alert(node.attributes[i].nodeValue);
15}
16
17if (node.hasChildNodes)
18{
19for (var i=0; i < node.childNodes.length; i++)
20walk(node.childNodes[i]);
21}
22
23
24}
25</script>
---------------------------------------------------------------
建议多看看xpath语法..
它提供了丰富的结点及结点集匹配的能力..
---------------------------------------------------------------
assume your xml is like this:
1<items>
2<aaa a="3" id="1" width="2">
3</aaa>
4<bbb id="4" width="5" z="6">
5</bbb>
6<ccc id="7">
7</ccc>
8</items>
try
1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2<xsl:template match="*">
3<xsl:for-each select="@*">
4<xsl:value-of select="local-name()"></xsl:value-of>=<xsl:value-of select="."></xsl:value-of>
5</xsl:for-each>
6<xsl:apply-templates></xsl:apply-templates>
7</xsl:template>
8</xsl:stylesheet>