www.webdeveloper.com
Results 1 to 4 of 4

Thread: Inserting XML into HTML dynamically

  1. #1
    Join Date
    Jul 2003
    Location
    Oxnard, CA
    Posts
    2

    Inserting XML into HTML dynamically

    I am trying to use the javascript DOM API to create dynamic HTML content. What I would like to do is get a portion of an xml document and insert it into an HTML document dynamically. The problem is, while I can insert TEXT all I want, I can't get copies of the elements and attributes themselves. Basically, I would like it to be like a cut and paste of part of my xml file. Here is some sample code:

    PHP Code:
    <html>
       <
    head>
          <
    title>XML Document Test</title>
       </
    head>
       <
    body>
          <
    p id="myID">Testing!</p>
          <
    script type="text/javascript">
            function 
    loadXML()
            {
               if(
    window.ActiveXObject)
               {
                          var 
    text document.forms[0].selector.options[document.forms[0].selector.selectedIndex].text;
                          var 
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                          
    xmlDoc.async false;
                          
    xmlDoc.load("data.xml");
                          
    document.getElementById("myID").innerHTML xmlDoc.getElementsByTagName(text)[0].firstChild.cloneNode(true);
                      }
                      else
                          
    document.write("error!");
                  }
          
    </script>
          
          <form>
                  <select onchange="loadXML()" id="selector">
                      <option>--pick one--</option>
                      <option>child1</option>
                      <option>child2</option>
                  </select>
          </form>
       </body>
    </html> 
    Now here's the XML file...
    PHP Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <child1>
            <b>This is the first child node.</b>
        </child1>
        <child2>
            <i>This is the second child node.</i>
        </child2>
    </root>

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    Hunmm
    Kinda confusing is the detail of your question.
    Care to rephrase it?
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Jul 2003
    Location
    Oxnard, CA
    Posts
    2
    I would like to take an element and it's child nodes from an xml document and place it in an element in an html document. So, if I had the following xml in a xml file:

    <dog>
    <name>Skip</name>
    <breed>Beagle</breed>
    </dog>

    and I wanted to take the <dog> element, along with it's child elements, <name> and <breed> and stick it into an html element dynamically, is there a way I could do that with javascript?

    lets say the html file looks kind of like this...

    ...
    <body>
    <div id="myDiv">This is where I want the "dog" element to end up.</div>
    </body>
    ...

    It would then end up like this:

    ...
    <body>
    <div id="myDiv">
    <dog>
    <name>Skip</name>
    <breed>Beagle</breed>
    </dog>
    </div>
    </body>
    ...

    Hope that clears things up a bit.

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    Take a look at the link I posted in this thread..

    http://forums.webdeveloper.com/showt...threadid=13881
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

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