Home > Back-end >  XML parentnode property not recognized and not working with xPath
XML parentnode property not recognized and not working with xPath

Time:12-22

I'm quite new on XML and JS, so please forgive me if the question is trivial. What I'm trying to do, is parsing an XML and collect all the nodes tagged with the value "name". After that, I want to put them in a table (I haven't developed the table part). Below is an excerpt from the parsed XML:

`

<?xml version="1.0" encoding="UTF-8"?>
<model-data>
    <identification>
        <date>Thu Dec 15 14:43:07 CET 2022</date>
        <xml-version>4.11</xml-version>
    </identification>
    <model>
        <named-domains>
            <named-domain>
                <name>boolean</name>
                <elements>
                    <element>
                        <value>0</value>
                        <name>No</name>
                        <description>No</description>
                    </element>
                    <element>
                        <value>1</value>
                        <name>Yes</name>
                        <description>Yes</description>
                    </element>
                </elements>
            </named-domain>
            <named-domain>
                <name>D_NOME_TECNICO_COMPLETO</name>
                <elements>
                    <element>
                        <value>0</value>
                        <name>PippoBaudo</name>
                        <description>Superstar</description>
                    </element>
                        ... // same pattern
                </elements>
            </named-domain>
        </named-domains>
    </model>
</model-data>

`

And here the JS code:

`

<!DOCTYPE html>
<html>

<body>

    <p id="demo"></p>

    <script>
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                showResult(xhttp.responseXML);
            }
        };
        xhttp.open("GET", "prova.xml", true);
        xhttp.send();

        function showResult(xml) {
            var txt = "";
            // path = "//named-domain/name[text()]";
            path = "/model-data/model/named-domains//name[text()]";
            if (xml.evaluate) {
                var nodes = xml.evaluate(path, xml, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
                var result = nodes.iterateNext();
                while (result) {
                        txt  = "<b>"   result.childNodes[0].nodeValue   "</b><br>";
                        result = nodes.iterateNext();
                        parente = nodes.parentnode.nodevalue; // <- Here something goes wrong!

                }
                // Code For Internet Explorer
            } else if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
                xml.setProperty("SelectionLanguage", "XPath");
                nodes = xml.selectNodes(path);
                for (i = 0; i < nodes.length; i  ) {
                    txt  = nodes[i].childNodes[0].nodeValue   "<br>";
                }
            }
            document.getElementById("demo").innerHTML = txt;

        }
    </script>
</body>

</html>

`

What I need, is to intercept the childnodes of nodes whose value is "named-domain" and to treat them in some ways. I tried to catch the parent node with "parente = nodes.parentnode.nodevalue" but:

  1. VS code doesn't suggest me the property parentnode for the element "nodes", and I don't understand why;
  2. If I *force *the property by hand, I receive the following error: "Uncaught TypeError: Cannot read properties of undefined (reading 'nodeValue')". Again: why?

Someone may help me?

Thank you

CodePudding user response:

It is parentNode, not parentnode. nodeValue in the DOM is not giving anything meaningful for element nodes.

  • Related