user495688
user495688

Reputation: 973

why i can't parse xml in javascript?

hello i have problem to parse xml.. i have xml like this :

<tejemahan>
  <kategori> komputer </kategori>
  <hasil> aplikasi komputer </hasil>
</terjemahan>

Edited: xml above I get in that way :

   var url="http://localhost:8080/inlinetrans/api/translate/"+userSelection+"/"+hasilStemSel+"/"+hasilStem;
   var client = new XMLHttpRequest();
   client.open("GET", url, false);
   client.setRequestHeader("Content-Type", "text/plain");
   client.send(null);

   if(client.status == 200)
       alert("the request success"+client.responseText);
   else
       alert("the request isn't success"+client.status+""+client.statusText)

  }

and this is my code to parse an xml file above :

this.loadXML = function (){
   var url = http://localhost:8080/coba/api/artikan/"+sel+"/"+hasilStemSel+"/"+hasilStem
   xmlDoc=document.implementation.createDocument("","",null);
   xmlDoc.load("url");
   xmlDoc.onload= this.readXML;
  }

  this.readXML = function() {
        alert(xmlDoc.documentElement.tagName);
 alert(xmlDoc.documentElement.childNodes[0].tagName);
 alert(xmlDoc.documentElement.childNodes[1].tagName);
 alert(xmlDoc.documentElement.childNodes[0].textContent);
 alert(xmlDoc.documentElement.childNodes[1].textContent);

     }

i can execute this code

xmlDoc=document.implementation.createDocument("","",null); 
xmlDoc.load("url");

but why i can't execute this code xmlDoc.load = this.readXML ???

Upvotes: 0

Views: 1706

Answers (2)

Tim Down
Tim Down

Reputation: 324717

Firstly, I second David Dorward's suggestion: use XMLHttpRequest instead, which will work in all major browsers. Code is below.

Secondly, your readXML function is flawed, since most browsers will include whitespace text nodes within the childNodes collection, so xmlDoc.documentElement.childNodes[0] will actually be a text node and have no tagName property. I would suggest using getElementsByTagName() or checking the nodeType property of each node as you iterate over childNodes.

Thirdly, your XML is not valid: the <tejemahan> and </terjemahan> do not match, although this may be a typo in your question.

var url = "http://localhost:8080/coba/api/artikan/"+sel+"/"+hasilStemSel+"/"+hasilStem;

var readXML = function(xmlDoc) {
    alert(xmlDoc.documentElement.tagName);
    var kategori = xmlDoc.getElementsByTagName("kategori")[0];
    alert(kategori.tagName);
};

var createXmlHttpRequest = (function() {
    var factories = [
        function() { return new XMLHttpRequest(); },
        function() { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); },
        function() { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); },
        function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
    ];

    for (var i = 0, len = factories.length; i < len; ++i) {
        try {
            if ( factories[i]() ) {
                return factories[i];
            }
        }
        catch (e) {}
    }
})();

var xmlHttp = createXmlHttpRequest();
xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        readXML(xmlHttp.responseXML);
    }
};

xmlHttp.open("GET", url, true);
xmlHttp.send(null);

Upvotes: 1

Zach
Zach

Reputation: 7940

Try putting the onload handler assignment before the load() call. If you call load() first, the onload event will happen before you have assigned a handler to handle it. Like this:

xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.onload= this.readXML;
xmlDoc.load("url");

Upvotes: 1

Related Questions