Click to See Complete Forum and Search --> : Inserting XML into HTML dynamically


willThomas
07-24-2003, 04:17 PM
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:


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


<?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>

Khalid Ali
07-24-2003, 09:55 PM
Hunmm:confused:
Kinda confusing is the detail of your question.
Care to rephrase it?

willThomas
07-25-2003, 05:20 AM
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.

Khalid Ali
07-25-2003, 12:57 PM
Take a look at the link I posted in this thread..

http://forums.webdeveloper.com/showthread.php?s=&threadid=13881