2017-04-09 2 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> 

Oben ist mein xml doc ich aus dem ‚url‘ -Attribut bekommen muss ich zuerst '<element:content/>' Tag versucht, die in w3schools.com erwähnten Möglichkeiten, aber ich hatteWie erhalte ich das Attribut Value eines XML-Tags, das mit Namespace erstellt wurde?

kein Glück etwas Hilfe sehr ich zugreifen müssen geschätzt it using javascript sorry für schlechte frage framing

Antwort

0

Sie können dies einfach mit dom manupulation erreichen.

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: "Sie können nicht mehrere Attribute mit dem gleichen Namen hinzufügen".

Sie die Lösung finden Sie hier: https://jsfiddle.net/vineetsagar7/3od130pd/2/

+0

Thank you Ich war nur, dass es zu erwähnen, versuchen, einige andere Attribute sind auch mit „otherattr“ Danke trotzdem für die Lösung – Devoper

Verwandte Themen