JavaScript rescursive XML元素遍历
我的目的是使用递归方法查找给定xml的所有“文本节点”,例如,如果输入xml为:JavaScript rescursive XML元素遍历,javascript,xml,algorithm,recursion,traversal,Javascript,Xml,Algorithm,Recursion,Traversal,我的目的是使用递归方法查找给定xml的所有“文本节点”,例如,如果输入xml为: <foo> <bar> <a>1</a> <b>2</b> </bar> </foo> 上述方法在大多数情况下都能正常工作,但如果父节点具有某些属性或xsi类型,则会失败。例如,如果下面是我的输入xml: <s
<foo>
<bar>
<a>1</a>
<b>2</b>
</bar>
</foo>
上述方法在大多数情况下都能正常工作,但如果父节点具有某些属性或xsi类型,则会失败。例如,如果下面是我的输入xml:
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<buyGuid>
<Context>
<INFO>
<buyGuidRequest>
<Property xsi:type="PostalAddress">
<address>Some address</address>
<postcode>0000</postcode>
</Property>
</buyGuidRequest>
</INFO>
</Context>
</buyGuid>
</soap:Body>
</soap:Envelope>
某个地址
0000
它不会遍历到
或
的子级!!但是,如果我从
标记中删除xsi:type
,它将正确地遍历到属性的所有子元素,并查找地址和zipCode。我不知道为什么会发生这种情况,以及xsi:type如何影响深度遍历?有人能解释一下这个问题吗?谢谢您的输入似乎格式不正确,无法识别xsi前缀。删除该属性时会复制该行为
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
从元素
某个地址
0000
什么是xml
,字符串还是文档?您需要使用jQuery吗?@Bergi我已经更新了代码,但是xml
指的是xml=$.parseXML(我的输入xml)代码>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<buyGuid>
<Context>
<INFO>
<buyGuidRequest>
<Property xsi:type="PostalAddress">
<address>Some address</address>
<postcode>0000</postcode>
</Property>
</buyGuidRequest>
</INFO>
</Context>
</buyGuid>
</soap:Body>
</soap:Envelope>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<buyGuid>
<Context>
<INFO>
<buyGuidRequest>
<Property xsi:type="PostalAddress">
<address>Some address</address>
<postcode>0000</postcode>
</Property>
</buyGuidRequest>
</INFO>
</Context>
</buyGuid>
</soap:Body>
</soap:Envelope>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<buyGuid>
<Context>
<INFO>
<buyGuidRequest>
<Property xsi:type="PostalAddress">
<address>Some address</address>
<postcode>0000</postcode>
</Property>
</buyGuidRequest>
</INFO>
</Context>
</buyGuid>
</soap:Body>
</soap:Envelope>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<buyGuid>
<Context>
<INFO>
<buyGuidRequest>
<Property>
<address>Some address</address>
<postcode>0000</postcode>
</Property>
</buyGuidRequest>
</INFO>
</Context>
</buyGuid>
</soap:Body>
</soap:Envelope>