开发者

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[开发者_JAVA百科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 ???


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");


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜