Fabian Boulegue
Fabian Boulegue

Reputation: 6608

Parse XML with Javascript

i m trying to parse a xml file into html. So i atm go with following

<script>
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET","test",false);
  xmlDoc.send("");
</script>

Now i want to "echo" the request, how do i do that

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</catalog>

Upvotes: 0

Views: 8681

Answers (3)

d4rkpr1nc3
d4rkpr1nc3

Reputation: 1827

Try this :

xmlDoc=new window.XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP") || new ActiveXObject("Msxml2.XMLHTTP");
xmlDoc.onreadystatechange = function(){
    if(xmlDoc.readyState = 4 && xmlDoc.status == 200)  // Success
    {
        document.write(xmlDoc.responseText);
    }
}
xmlDoc.open("GET","test",false);
xmlDoc.send("");

Upvotes: 1

Kevinatclutr
Kevinatclutr

Reputation: 84

You could either alert('your data or text here') the data and display it in a popup, or select a div using any variety of methods and use .html(data) to insert your data.

Upvotes: 0

Ωmega
Ωmega

Reputation: 43663

Parse an XML String

The following code fragment parses an XML string into an XML DOM object:

txt="<bookstore><book>";
txt=txt+"<title>Everyday Italian</title>";
txt=txt+"<author>Giada De Laurentiis</author>";
txt=txt+"<year>2005</year>";
txt=txt+"</book></bookstore>";

if (window.DOMParser)
{
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
}
else // Internet Explorer
{
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(txt); 
}

Parse an XML Document

The following code fragment parses an XML document into an XML DOM object:

if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else
{ // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","books.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

Upvotes: 0

Related Questions