CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   Extracting contents of CDATA section in XML using Javascript (http://www.codingforums.com/showthread.php?t=191529)

shanker.nair 03-15-2010 09:12 AM

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

rnd me 03-15-2010 09:57 AM

Quote:

Originally Posted by shanker.nair (Post 932804)
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;


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

Quote:

Originally Posted by shanker.nair (Post 932830)
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 -

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

Quote:

Originally Posted by rnd me (Post 933107)
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

RandiR 04-07-2010 04:04 PM

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 .

Kor 02-11-2011 12:09 PM

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


All times are GMT +1. The time now is 08:43 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.