Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2010, 09:12 AM   PM User | #1
shanker.nair
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
shanker.nair is an unknown quantity at this point
Question 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
shanker.nair is offline   Reply With Quote
Old 03-15-2010, 09:57 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,549
Thanks: 9
Thanked 479 Times in 462 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by shanker.nair View Post
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[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:

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:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 03-15-2010, 10:28 AM   PM User | #3
shanker.nair
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
shanker.nair is an unknown quantity at this point
Hi rnd me,

Thanks for the help.
But, I am not able to extract the contents of the <howout> tag.
Please help.

Regards,
Shanker
shanker.nair is offline   Reply With Quote
Old 03-15-2010, 11:20 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,549
Thanks: 9
Thanked 479 Times in 462 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by shanker.nair View Post
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:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 03-16-2010, 09:19 AM   PM User | #5
shanker.nair
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
shanker.nair is an unknown quantity at this point
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
shanker.nair is offline   Reply With Quote
Old 03-16-2010, 09:28 AM   PM User | #6
shanker.nair
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
shanker.nair is an unknown quantity at this point
Quote:
Originally Posted by rnd me View Post
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
shanker.nair is offline   Reply With Quote
Old 04-07-2010, 04:04 PM   PM User | #7
RandiR
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
RandiR is an unknown quantity at this point
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 .
RandiR is offline   Reply With Quote
Old 02-11-2011, 12:09 PM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 AM.


Advertisement
Log in to turn off these ads.