Enjoy an ad free experience by logging in. Not a member yet?
Register .
03-15-2010, 09:12 AM
PM User |
#1
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Extracting contents of CDATA section in XML using Javascript
Hi All,
I need to extract contents of CDATA section in XML using Javascript. The XML looks like this
Code:
<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
03-15-2010, 09:57 AM
PM User |
#2
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
Quote:
Originally Posted by
shanker.nair
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:
Code:
- <![CDATA[ c ]]>
- <![CDATA[I cII ]]>
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
:
Code:
function trim(s){ return String(s).replace(/^\s+|\s+$/g,''); }
usage:
Code:
var string=" abc ";
var trimmed=trim(string);
alert( trimmed.length)//==3;
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
03-15-2010, 10:28 AM
PM User |
#3
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi rnd me,
Thanks for the help.
But, I am not able to extract the contents of the <howout> tag.
Please help.
Regards,
Shanker
03-15-2010, 11:20 PM
PM User |
#4
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
Quote:
Originally Posted by
shanker.nair
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
03-16-2010, 09:19 AM
PM User |
#5
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi rnd me,
Herez the code -
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
03-16-2010, 09:28 AM
PM User |
#6
New to the CF scene
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
rnd me
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 -
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 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
04-07-2010, 04:04 PM
PM User |
#7
New to the CF scene
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Extracting CDATA
Hi Shanker,
Consider these couple of biterscripting commands. They will retrieve the "c" from CDATA.
Code:
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 .
02-11-2011, 12:09 PM
PM User |
#8
Red Devil Mod
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
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
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 12:15 AM .
Advertisement
Log in to turn off these ads.