Reputation: 591
I have the following bit of XML. 2 questions which I can seem to anwser: Is it valid XML? (To have a value and children nodes inside 'FOO'. Question 2 (The more important one), How do I access the 'TEXT' value and the 'BOO' node separately in JavaScript?
<FOO>
TEXT
<BOO>
</BOO>
<BOO>
</BOO>
</FOO>
Tried .firstChild.nodeValue and this works fine. (Returned the value of TEXT) however the problem occurs if TEXT is null. It returns a [Object Element] instead (the BOO node). So basically - I want to get the TEXT text if its there, if not I want to get null, or equivalent.
Upvotes: 0
Views: 156
Reputation: 4365
Yes it is a valid XML. This are some basic rule for a valid XML:
Having this XML document:
<CATALOG>
VERSION_1
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
You can access the document in JavaScript using some code like this:
x=xmlDoc.getElementsByTagName("CD");
i=0;
function displayCD()
{
// What you want here --->
textInRootElemnt=xmlDoc.getElementsByTagName("CATALOG")[0].childNodes[0].nodeValue;
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
Where xmlDoc is your loaded document.
Consider the following very basic tutorial about XML.
Upvotes: 2
Reputation: 23
Yes, it is valid xml.
Please rephrase your question clearly : How do I access the 'TEXT' and the separately in JavaScript?
Upvotes: 0