<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文档,我需要获取&#39; url&#39;来自第一个'<element:content/>'
标签的属性我尝试了w3schools.com中提到的方法,但我有
没有运气一些帮助非常感谢我需要使用javascript访问它抱歉不好的问题框架
答案 0 :(得分:0)
您可以使用dom manupulation简单地实现此目的。
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/