View Full Version : Extracting contents of CDATA section in XML using Javascript
shanker.nair
03-15-2010, 09:12 AM
Hi All,
I need to extract contents of CDATA section in XML using Javascript. The XML looks like this
<batsmen order="6">
<fullname>S.Gosy</fullname>
<shortname>S.Gosy</shortname>
<batsmanid>4343</batsmanid>
<runs>8</runs>
<balls>10</balls>
<fours>2</fours>
<sixes>0</sixes>
<minutes />
- <howout>
- <![CDATA[ c ]]>
</howout>
<bowler>Langy</bowler>
<fielder>Shamy</fielder>
</batsmen>
Am at my wits end trying to extract the value inside CDATA which is "c". There seem to be spaces before and after "c".
Please help.
Regards,
Shanker
rnd me
03-15-2010, 09:57 AM
Am at my wits end trying to extract the value inside CDATA which is "c". There seem to be spaces before and after "c".
uh, there are spaces inside the code you pasted:
- <![CDATA[ c ]]>
- <![CDATA[IcII]]>
if you dont need spaces, don't use unparsed CDATA section.
if you can't control the data, you can still clean it up in javascript:
function trim(s){ return String(s).replace(/^\s+|\s+$/g,''); }
usage:
var string=" abc ";
var trimmed=trim(string);
alert( trimmed.length)//==3;
shanker.nair
03-15-2010, 10:28 AM
Hi rnd me,
Thanks for the help.
But, I am not able to extract the contents of the <howout> tag.
Please help.
Regards,
Shanker
rnd me
03-15-2010, 11:20 PM
Hi rnd me,
Thanks for the help.
But, I am not able to extract the contents of the <howout> tag.
Please help.
Regards,
Shanker
sorry, i thought you got it but it had spaces around it.
post the code you're using and i'll tell you what's wrong.
shanker.nair
03-16-2010, 09:19 AM
Hi rnd me,
Herez the code -
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest()
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xhttp.open("GET","ScoreCard.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
var temp=xmlDoc.getElementsByTagName("howout")[0];
if(temp.childNodes[0])
{
var tempvalue = temp.childNodes[0];
alert("Value in tempvalue: " + tempvalue); // shows [object Text]
alert("Length: " + tempvalue.length); // shows 17
alert("Value: " + tempvalue.nodeType); // shows 3
}
else
{
alert("No Value in tags !");
}
I need to extract the "c" within <![CDATA[ c ]]>.
Please help.
Thanks.
Regards,
Shanker
shanker.nair
03-16-2010, 09:28 AM
sorry, i thought you got it but it had spaces around it.
post the code you're using and i'll tell you what's wrong.
Hi rnd me,
Herez the code that was used to parse the <howout> tag -
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest()
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xhttp.open("GET","ScoreCard.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
var temp=xmlDoc.getElementsByTagName("howout")[0];
if(temp.childNodes[0])
{
var tempvalue = temp.childNodes[0];
alert("Value in tempvalue: " + tempvalue); // Shows Value in tempvalue: [object Text]
alert("Length: " + tempvalue.length); // Shows Length: 17
alert("Value: " + tempvalue.nodeType); // Shows Value: 3
}
else
{
alert("No Value in tag!");
}
I need to extract the "c" in <![CDATA[ c ]]>.
Please help.
Regards,
Shanker
RandiR
04-07-2010, 04:04 PM
Hi Shanker,
Consider these couple of biterscripting commands. They will retrieve the "c" from CDATA.
var str xml, cdata
cat "/path/to/xml/file.xml" > $xml
stex -c "^<![CDATA[^]" $xml > null ; stex -c "[^]]> ^" $xml > null
# The CDATA "c" is now in $xml.
echo $xml
stex (string extractor) command is at http://www.biterscripting.com/helppages/stex.html .
CDATA sections are also nodes of the DOM. You must simply circle through nodes and see which one has the property nodeType==4 and retrieve the text inside as being its firstChild.NodeValue
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.