GetElementById for XML Documents, Mozilla Extensions

Is a method supported document.getElementByIdin the DOM parsed from XML strings using the DOMParser method in Mozilla? I am making a Mozilla extension that reads an XML file and uses the DOM Parser to convert the XML to a DOM element and tries to get the elements by Id. The getElementsByTagName method works, but not getElementById. Always returns zero.

function (xmlString) {
    var parser = new DOMParser();
    var doc = parser.parseFromString(xmlString, "text/xml"); 
    var aNodes = doc.getElementsByTagName("nodeTag");
    for(var i=0; i<aNodes.length; ++i) {
        var id = aNodes[i].getAttribute('id');
        var resultNode = doc.getElementById(id);
        alert(id);
        alert(resultNode);
    }
}

I tried the code above. alert (id) returns the correct identifier, while alert (resultNode) returns null each time.

+3
source share
2 answers

, document.getElementById XML-.

(, Firefox 3.5 ) document.querySelector:

var resultNode = doc.querySelector("[id=" + id + "]");
+8

, getElementById() ( , ), DTD, :

<!ATTLIST client id ID #IMPLIED >

DTD, xml <?xml version= \"1.0\"?>:

<!DOCTYPE clients [ 
   <!ATTLIST client id ID #IMPLIED > 
]>

"" - xml , "" - , . "ATTLIST" , id .

, firefox. .

+2

Source: https://habr.com/ru/post/1763777/


All Articles