How to find a specific parent node?

I need to convert docx to xml and use the DOM to find the parent node specific.

for example

   <chapter>
       <title>
           <label>Chapter 1
           </label>
       </title>
       <para>This is chapterpara <a id="book"/>
       </para>
   </chapter>

Here I want to find an anchor node a super parent child (node ​​header).

I knew the link to the node anchor, getting

 var itemanchor=ItemElement.getElementsByTagName('a');

How to cross a specific parent node from the above help?

Thanks in advance.

+4
source share
1 answer

Use a method createDocumentFragment()to create a fake document, and then add html to fake the document and get the target element from it. See this example.

var HTML =
'<chapter>\
    <title>\
        <label>Chapter1</label>\
    </title>\
    <para>\
        This is chapterpara\
        <a id="book" href="mySiteAddress" />\
    </para>\
</chapter>';

var element = document.createElement('div');
element.innerHTML = HTML;

var docfrag = document.createDocumentFragment();
docfrag.appendChild(element);

var href = docfrag.getElementById("book").getAttribute("href");
document.write(href);
Run codeHide result

If you want to get another item, you need to get the item from docfrag.

+1

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


All Articles