【问题标题】:Issue with conversion of xml to JSON将xml转换为JSON的问题
【发布时间】:2013-03-09 04:43:34
【问题描述】:

我遵循以下教程 - http://davidwalsh.name/convert-xml-json 并收到错误:

Uncaught exception: TypeError: 'xml.hasChildNodes' is not a function.

我不确定如何解决这个问题?许多人在网站上发布了同样的内容。但没有运气。

请帮忙。

代码在这里:

<!DOCTYPE html>  
<html lang="en">  
<head>  
<title>Welcome</title>  

<script type="text/javascript">  
function myFun()  
{  
    alert("hi..");  
    var jsonText = JSON.stringify(xmlToJson("D:/Tech/data.xml"));  
    for(var i in jsonText)  
   {
        alert(i);
   }
}   

// Changes XML to JSON  
function xmlToJson(xml) {  

   // Create the return object  
   var obj = {};  

    if (xml.nodeType == 1) { // element  
        // do attributes  
        if (xml.attributes.length > 0) {  
        obj["@attributes"] = {};  
            for (var j = 0; j < xml.attributes.length; j++) {  
                var attribute = xml.attributes.item(j);  
                obj["@attributes"][attribute.nodeName] = attribute.nodeValue;  
            }  
        }  
     } else if (xml.nodeType == 3) { // text  
        obj = xml.nodeValue;  
    }

     // do children   
    if (xml.hasChildNodes()) {  
        for(var i = 0; i < xml.childNodes.length; i++) {  
            var item = xml.childNodes.item(i);  
            var nodeName = item.nodeName;  
            if (typeof(obj[nodeName]) == "undefined") {  
                obj[nodeName] = xmlToJson(item);  
             } else {  
                 if (typeof(obj[nodeName].push) == "undefined") {  
                    var old = obj[nodeName];  
                    obj[nodeName] = [];  
                    obj[nodeName].push(old);  
                }  
                 obj[nodeName].push(xmlToJson(item));  
            }  
        }  
    }  
    return obj;  
};  

 </script>  

</head>  

<body onload="myFun()">  
 Hello  
</body>  
</html>  

data.xml 是这样的:

<applications>  
<application id="backupparking">  
  <toplevel />   
 <contexts>  
 <context id="Idle" />   
 <context id="ABCD" />   
 <context id="EFGH" />   
 <context id="LMN">  
 <payload>  
  <member id="hi" type="string" />   
  </payload>  
  </context>  
  </application>  
  </applications>  

谢谢!

【问题讨论】:

  • 如果您需要帮助,请设置小提琴。
  • 我不是 javascript 专家,但您不是将 filename 传递给 xmlToJson 方法吗?当然,您应该首先将 xml 从磁盘读取到 XML 对象中,然后该对象可能具有相关功能。
  • @Sneha - 您必须将 xml 字符串输入 xmlToJson 函数而不是 xml 文件路径...
  • @shunty & Coder:是的!你说的对。最好的方法是什么?我该如何进行?

标签: xml json parsing


【解决方案1】:

contexts 标签未正确关闭。

试试

<applications>  
<application id="backupparking">  
  <toplevel />   
 <contexts />  
 <context id="Idle" />   
 <context id="ABCD" />   
 <context id="EFGH" />   
 <context id="LMN">  
 <payload>  
  <member id="hi" type="string" />   
  </payload>  
  </context>  
  </application>  
  </applications> 

或者

<applications>  
<application id="backupparking">  
  <toplevel />   
 <contexts>  
 <context id="Idle" />   
 <context id="ABCD" />   
 <context id="EFGH" />   
 <context id="LMN">  
 <payload>  
  <member id="hi" type="string" />   
  </payload>  
  </context>  
 </contexts>  
  </application>  
  </applications> 

【讨论】:

  • 这只是一个示例。我的 xml 文件已经过验证,没问题。这不是我觉得的问题:(
猜你喜欢
  • 2022-01-22
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 2014-11-26
  • 2014-03-27
  • 2016-10-21
  • 2014-08-11
  • 1970-01-01
相关资源
最近更新 更多