为什么我无法在 javascript 中解析 xml?

发布于 2024-10-12 16:49:12 字数 1489 浏览 2 评论 0原文

你好,我在解析 xml 时遇到问题.. 我有这样的 xml:

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

编辑: 上面的 xml 我就是这样得到的:

   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)

  }

这是我解析上面 xml 文件的代码:

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

     }

我可以执行这段代码

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

,但为什么我不能执行这段代码 xmlDoc.load = this.readXML ???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日久见人心 2024-10-19 16:49:12

尝试将 onload 处理程序分配放在 load() 调用之前。如果您先调用 load(),onload 事件将在您分配处理程序来处理它之前发生。像这样:

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

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");
幸福还没到 2024-10-19 16:49:12

首先,我赞同 David Dorward 的建议:使用 XMLHttpRequest 代替,它适用于所有主流浏览器。代码如下。

其次,您的 readXML 函数存在缺陷,因为大多数浏览器都会在 childNodes 集合中包含空白文本节点,因此 xmlDoc.documentElement.childNodes[0] 实际上是一个文本节点,并且没有 tagName 属性。我建议在迭代 childNodes 时使用 getElementsByTagName() 或检查每个节点的 nodeType 属性。

第三,您的 XML 无效: 不匹配,尽管这可能是您问题中的拼写错误。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文