Phonegap构建XML请求

时间:2013-11-14 14:57:47

标签: javascript android html xml cordova

我使用fallowing代码从XML文件中获取信息。 它可以在我的笔记本电脑上工作,并直接使用文件浏览器加载html文件。 但是它不适用于phonegap build创建的应用程序。 我正在使用android 4.3。希望有人知道答案。

var Connect = new XMLHttpRequest()
Connect.open("GET", "data/test.xml", false);
Connect.setRequestHeader("Content-Type", "text/xml");
Connect.send(null);
var TheDocument = Connect.responseXML;
var workid = TheDocument.childNodes[0];
for (var i = 0; i < workid.children.length; i++)
{
var id = workid.children[i];
var Name = id.getElementsByTagName("title");
document.write(Name[0].textContent.toString());
  }

希望你能帮忙!

1 个答案:

答案 0 :(得分:0)

对于遇到同样问题的其他人:使用以下代码使其工作。

<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
} 
</script>

</head>

<body>

<div class="frame">

<script>
xmlDoc=loadXMLDoc("test.xml");
x=xmlDoc.getElementsByTagName("title");
document.write(x[0].childNodes[0].nodeValue);
alert('loaded');
</script>

</div>

</body>
相关问题