admin管理员组文章数量:1394772
I have an XML document of the following format:
<root>
<item>
<type>link</type>
<name>Nyan Cat!</name>
<author></author>
<content>/</content>
</item>
<item>
<type>quote</type>
<name>Belief and Intelligence</name>
<author>Robert Anton Wilson</author>
<content>Belief is the death of intelligence.</content>
</item>
</root>
As you can see, all item
tags have the children type, name, author, content
, however for some cases, the author
tag might contain an empty #text
child.
In a Javascript file, I have the following code to get the text values of these tags from the item
DOM element:
this.type = item.getElementsByTagName("type")[0].childNodes[0].nodeValue;
this.name = item.getElementsByTagName("name")[0].childNodes[0].nodeValue;
this.author = item.getElementsByTagName("author")[0].childNodes[0].nodeValue;
this.content = item.getElementsByTagName("content")[0].childNodes[0].nodeValue;
The variable item
is the DOM element for the <item>
tag. The code runs fine when the author is non-empty, but when author
is empty, the code does not run. How do I solve this? If author
is empty, then I want its node value to be the empty string ""
.
I have an XML document of the following format:
<root>
<item>
<type>link</type>
<name>Nyan Cat!</name>
<author></author>
<content>http://nyan.cat/</content>
</item>
<item>
<type>quote</type>
<name>Belief and Intelligence</name>
<author>Robert Anton Wilson</author>
<content>Belief is the death of intelligence.</content>
</item>
</root>
As you can see, all item
tags have the children type, name, author, content
, however for some cases, the author
tag might contain an empty #text
child.
In a Javascript file, I have the following code to get the text values of these tags from the item
DOM element:
this.type = item.getElementsByTagName("type")[0].childNodes[0].nodeValue;
this.name = item.getElementsByTagName("name")[0].childNodes[0].nodeValue;
this.author = item.getElementsByTagName("author")[0].childNodes[0].nodeValue;
this.content = item.getElementsByTagName("content")[0].childNodes[0].nodeValue;
The variable item
is the DOM element for the <item>
tag. The code runs fine when the author is non-empty, but when author
is empty, the code does not run. How do I solve this? If author
is empty, then I want its node value to be the empty string ""
.
2 Answers
Reset to default 4I think you should check how many childNodes does the author have:
if (item.getElementsByTagName("author")[0].childNodes.length == 0) {
this.author = '';
} else {
this.author = item.getElementsByTagName("author")[0].childNodes[0].nodeValue;
}
You have to check for "undefined" on each property of the tree. As in:
if( typeof (element) == "undefined" ){
//set var to empty string
}
If you try to access a property of a javascript that is null or undefined, the script will fail at that line and not execute any lines after it.
To get around failing, you can wrap those blocks of code that may fail in a try{}catch(e){}
本文标签: javascriptHandle Empty text in XML DOMStack Overflow
版权声明:本文标题:javascript - Handle Empty #text in XML DOM - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744102811a2590943.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论