2017-04-09 53 views
0
<root xmlns='http://www.w3.org/2005/Atom' xmlns:element='http://search.yahoo.com/mrss/'> 
<element:group> 
<element:content url='https://myrequiredurl.com' otherattr='360' otherattr='640' otherattr='somthng' medium='smtng'/> 
<element:content url='https://myrequiredurl.com' otherattr='720' otherattr='1280' otherattr='smtng' medium='smtng'/> 
<element:content url='https://myrequiredurl.com' otherattr='1080' otherattr='1920' otherattr='smtng' medium='smtng'/> 
</element:group> 
</root> 

以上是我的XML文档,我需要从第一'<element:content/>'标签获得“网址”属性,我试图在w3schools.com提到的方式,但我不得不如何获取使用名称空间创建的xml标签的属性值?

没有运气有些帮助深表感谢我需要访问它使用JavaScript对不起的问题框架

回答

0

你可以简单地实现这个使用DOM管理。

HTML:

<p id="show"></p> 

的Javascript:

var urlList = []; 
    var txt = "<root xmlns='http://www.w3.org/2005/Atom' xmlns:element='http://search.yahoo.com/mrss/'>" + 
    "<element:group>" + 
    "<element:content url='https://1myrequiredurl.com' otherattr='somthng' medium='smtng'> </element:content>" + 
    "<element:content url='https://2myrequiredurl.com' otherattr='smtng' medium='smtng'> </element:content>" + 
    "<element:content url='https://3myrequiredurl.com' otherattr='smtng' medium='smtng'> </element:content>" + 
    "</element:group>" + 
    "</root>"; 
    if (window.DOMParser) { 
    parser = new DOMParser(); 
    xmlDoc = parser.parseFromString(txt, "text/xml"); 
    } 
    else // For Internet Explorer 
    { 
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xmlDoc.async = false; 
    xmlDoc.loadXML(txt); 
    } 

    var group = xmlDoc.getElementsByTagName("root")[0].childNodes[0].childNodes; 

    for (var content in group) { 
    if (!isNaN(content)) { 
     var url = group[content].getAttribute("url") 
     urlList.push(url); 
    } 
    } 

document.getElementById("show").innerHTML = urlList.join("</br>"); 

P.S: “你不能使用相同的名称添加多个属性”。

,你可以在这里找到解决方案:https://jsfiddle.net/vineetsagar7/3od130pd/2/

+0

谢谢你,我只是想提一下,还有一些其他的属性也有“otherattr”无论如何谢谢你的解决方案 – Devoper

相关问题