2015-10-06 68 views
1

我有一个xml文件和一些标签有点像这样<m:properties></m:properties> 但是当我尝试读取它不起作用。XML <example>标记有:(<m:example>)

这是我的代码如何阅读:

displayCD(0); 

function displayCD(i) { 
var xmlhttp = new XMLHttpRequest(); 
xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     myFunction(xmlhttp, i); 
    } 
} 
xmlhttp.open("GET", "text.xml", true); 
xmlhttp.send(); 
} 

function myFunction(xml, i) { 
var xmlDoc = xml.responseXML; 
x = xmlDoc.getElementsByTagName("ENTRY"); 
document.getElementById("text").innerHTML = 
"Artist: " + 
x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue + 
"<br>Title: " + 
x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue + 
"<br>Year: " + 
x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + 
"<br>Price: " + 
x[i].getElementsByTagName("body")[0].childNodes[0].nodeValue + 
"<br>Price: " + 
x[i].getElementsByTagName("expires")[0].childNodes[0].nodeValue; 
} 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<feed> 
    <ENTRY> 
     <content> 
      <m:properties> 
       <id>41</id> 
       <type>Hallo meneer</type> 
       <title>MAILING</title> 
       <body>Just some random content Hi !</body> 
       <expires>2013-07-11</expires> 
      </m:properties> 
     </content> 
    </ENTRY> 
</feed> 

,如果我删除了:它的工作原理完全没问题,反正是有这样我可以保持:仍然读它?

+2

那就是所谓的xml命名空间 – Nanoc

+0

可以显示xml吗? –

+0

@ T.G我编辑了这篇文章。 – SjaakvBrabant

回答

1

XML不是well-formed。一种使其良构的方法是从m:properties中删除命名空间前缀(m:)。

另外,保持m:,声明命名空间前缀:

<feed xmlns:m="http://www.example.com/m"> 

没有任何消除命名空间前缀或宣布它的XML不能很好地形成并不会被兼容的XML解析器成功解析。