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
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading CDATA Information from an XML tag

    How would I go about using the DOM to read (and return) the value of an XML tag whose contents are wrapped in a CDATA section?

    Currently, I'm pulling the data this way from non-CDATA tags:
    Code:
    xmlDoc.getElementsByTagName('content')[0].firstChild.data;

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Depends on the DOM implementation. Some DOM implementations are bound to have inserted the CDATA section as a TextNode. Some may have the CDATA node there, though. In either case, either the data or the nodeValue property should contain the textual contents of it.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    data and nodeValue aren't returning anything (I'm getting blanks). I know that the rest of my code is fine, because I can parse the exact same tag without the CDATA there.

    Would something like innerText or whatever work? (I'm dealing exclusively with Gecko 1.4 or newer)

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you're dealing with Gecko exclusively, then you could probably just use a Range:

    var r = xmlDoc.createRange();
    r.selectNode(xmlDoc.getElementsByTagName("content")[0]);
    alert(r.toString());

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Skyzyx: Have you checked in the DOM inspector how the JavaScript object looks? You know, there might be whitespace nodes in between, which breaks your use of firstChild. Also, it might be that you need to normalise the content first. (Should eliminate all problems you might have with in-between nodes, if that's what the problem is really about.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How would you normalize the content?

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeň, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    [object Element].normalize();
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Y'know what? It was a whitespace issue. Thanks for pointing me in the right direction guys.


  •  

    Posting Permissions

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