admin管理员组文章数量:1292149
I have the following code where i'm trying to get the c:CreationDate nodes:
value = '<?xml version="1.0" encoding="UTF-8"?><content><c:CreationDate>2010-09-04T05:04:53Z</c:CreationDate></content>';
xml = (new DOMParser()).parseFromString(value, 'text/xml');
console.log(xml.getElementsByTagName('c:CreationDate'));
Unfortunately it's returning an empty array, instead of an array with the node that is in the xml. I think that this is caused because of the ":" symbol.
Is there a way to escape it?
Note: Please, do not suggest usage on childNodes or things like this. This will not work for me since my xml is verify plex ( here is just a sample ) and will most likely change in the future and i can only rely in tag name.
Thanks!
I have the following code where i'm trying to get the c:CreationDate nodes:
value = '<?xml version="1.0" encoding="UTF-8"?><content><c:CreationDate>2010-09-04T05:04:53Z</c:CreationDate></content>';
xml = (new DOMParser()).parseFromString(value, 'text/xml');
console.log(xml.getElementsByTagName('c:CreationDate'));
Unfortunately it's returning an empty array, instead of an array with the node that is in the xml. I think that this is caused because of the ":" symbol.
Is there a way to escape it?
Note: Please, do not suggest usage on childNodes or things like this. This will not work for me since my xml is verify plex ( here is just a sample ) and will most likely change in the future and i can only rely in tag name.
Thanks!
Share asked Nov 26, 2010 at 20:01 FernandoFernando 4,6294 gold badges28 silver badges40 bronze badges1 Answer
Reset to default 12The 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.
本文标签: Javascript xml parser how to get nodes that have quotquot in the nameStack Overflow
版权声明:本文标题:Javascript xml parser: how to get nodes that have ":" in the name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741550061a2384825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论