www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to parse CDATA using DOM parser?

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    How to parse CDATA using DOM parser?

    How to parse CDATA using DOM parser?

    Here is the xml:
    <?xml version="1.0" encoding="UTF-8" ?>
    <abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="H:\abc.xsd"><body><payload>
    <![CDATA[<Envelope xmlns="http://www.department.com/abc">
    <Body>
    <Company>
    <Employee name="">
    <Employee employeeid="123" versionId="1">1440848757001</Employee >
    <EventCd reasonCd="direct">VALID</EventCd>
    </Employee >
    </Company>
    </Body>
    </Envelope>]]></payload></body></abc>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You may simply parse it as a simple content of a child node (using the property: data)

    in JavaScript (+-AJAX)
    Code:
    var content=xmldocument.documentElement.getElementsByTagName('payload')[0].firstChild.data;
    To prevent a crossbrowser problem regarding the counting of the child nodes (IE vs other browsers) you may use a small function and check the nodeType:

    Code:
    var content=returnCDATA(xmldocument.documentElement.getElementsByTagName('payload')[0].firstChild);
    function returnCDATA(elem){
    while(elem.nodeType!=4){
    elem=elem.firstChild;
    }
    return elem.data;
    }
    Last edited by Kor; 04-20-2012 at 07:28 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles