Home > Mobile >  How could I reach a specific tag while using DOM?
How could I reach a specific tag while using DOM?

Time:10-14

Please show me how to reach "autovehicul" as result, while language is english and its sibling is car. Thank you. If language english is selected and car is selected, in a web service, my answer must be autovehicul, if romanian language exists.

How can you parse that xml to reach an answer like that. I have an issue with targeting a specific node. Condition - Nodelist - Iterate - Condition - Node - get Tag. Then, i have to establish the node of autovehicul content, and get the required result.

<Word>
    <equivalent>
        <language>english</language>
        <word-translate>car</word-translate>
    </equivalent>

    <equivalent>
        <language>romanian</language>
        <word-translate>autovehicul</word-translate>
    </equivalent>
</Word>

CodePudding user response:

I don't really understand the question, but I think you might looking for something like this: link

CodePudding user response:

You can try something like this:

<!DOCTYPE html>
<html>
<body>

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

<script>
var parser, xmlDoc;
var text = "<Word> <equivalent><language>english</language><word-translate>car</word-translate></equivalent>"  
"<equivalent><language>romanian</language><word-translate>autovehicul</word-translate></equivalent></Word>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("word-translate")[1].childNodes[0].nodeValue;
</script>

</body>
</html>

Or you can try XPath for an easier access.

  • Related