SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parse (x)HTML content from XML (AJAX and DOM)

    Hi guys,

    I have a script that reads the contents of an XML file.
    The contents of one of the tags of the XML file contains XHTML(made up of <p> and <a> tags).

    The code I have to output the content of the XML is as follows:
    Code:
    xmlRoot = xmlResponse.documentElement;
    
    var wrapper        = document.getElementById("myDiv");
    var bodyText     = xmlRoot.getElementsByTagName("bodyText");
    
    for(var i=0; i<bodyText.length; i++)
    {
        var para         = document.createElement("p");
        var bodyContent = document.createTextNode(bodyText[i].firstChild.data);
        
        para.appendChild(bodyContent);
        wrapper.appendChild(para);    
        ...
    }
    Now, when I view the page in my browser, I get paragraphs that actually display the HTML code.

    Is there a way to parse this HTML so that it actually appears with all the links etc, as opposed to just showing the code?

    Does this make sense?

    D

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about you cheat slightly...

    var para = document.createElement("p");
    para.innerHTML = bodyText[i].firstChild.data;

  3. #3
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ... thanks for the reply ... although that works, is it considered really bad practice?


    D

  4. #4
    SitePoint Member El Fuego's Avatar
    Join Date
    Dec 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only other way to do it would be to extract each HTML element and do .createElement and .setAttribute as necessary - very long-winded.
    --------------
    El Fuego
    --------------

  5. #5
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ... yeah, thats the way i thought it colod have been done, but probably more trouble than its worth?


Bookmarks

Posting Permissions

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