Javascript xml parser: how to get nodes that have “:” in the name

浪子不回头ぞ 提交于 2019-12-04 15:38:59

The c in c:CreationDate denotes an XML namespace prefix. The namespace prefix is only a shortcut for the namespace. The namespace has to be defined somewhere in the document with an xmlns:c attribute. But in your document the namespace definition is missing.

So it should look like:

var value = '<?xml version="1.0" encoding="UTF-8"?>' +
            '<content>' +
            '  <c:CreationDate xmlns:c="http://my.namespace">2010-09-04T05:04:53Z</c:CreationDate>' +
            '</content>';

or

var value = '<?xml version="1.0" encoding="UTF-8"?>' +
            '<content xmlns:c="http://my.namespace">' +
            '  <c:CreationDate>2010-09-04T05:04:53Z</c:CreationDate>' +
            '</content>';

In this example the prefix c is assigned to the namespace http://my.namespace. The CreationDate tag is prefixed with c, so it belongs to the namespace http://my.namespace.

Then you can use the namespace aware getElementsByTagNameNS() function to query for the CreationDate element:

console.log(xml.getElementsByTagNameNS('http://my.namespace', 'CreationDate'));

As the first parameter you have to pass the real namespace name and not the prefix.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!