Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #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

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #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

  • #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 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

  • #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 .

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •