0

Im trying to print out the content of an xml string but can't get it to work. Anyone that can see my misstake?

XML string:

<row>
    <text>Hello</text>
    <value>1</value>
</row>
<row>
    <text>Hello2</text>
    <value>2</value>
</row>
<row>
    <text>Hello3</text>
    <value>3</value>
</row>

The XML is a string that is sent to showXML function.

Javascript:

function showXML(xmlText) {
    var ele = document.getElementById("Content");
    ele.style.display = "block";
    var doc = StringtoXML(xmlText); //Coverts string to XML

    var html = "";

    var rows = doc.getElementsByTagName("row");
    for (var i = 0; i < rows.length; i++) {     
        var text = rows[i].getElementsByTagName("text").nodeValue;
        var value= rows[i].getElementsByTagName("value").nodeValue;  
        html = html + " text: " + text + " value: " +value;   
    }  
    ele.innerHTML = html;
} 

function StringtoXML(text){
if (window.ActiveXObject){
    var doc=new ActiveXObject('Microsoft.XMLDOM');
    doc.async='false';
    doc.loadXML(text);
} else {
    var parser=new DOMParser();
    var doc=parser.parseFromString(text,'text/xml');
}
return doc;
}

Thx for all the help!

1 Answer 1

1

Here are the correction required in your for loop

    var text = rows[i].getElementsByTagName("text")[0].childNodes[0].nodeValue;
    var value= rows[i].getElementsByTagName("value")[0].childNodes[0].nodeValue;  

getElementsByTagName returns an array/collection.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you using the the code as below: for (var i = 0; i < rows.length; i++) { var text = rows[i].getElementsByTagName("text")[0].childNodes[0].nodeValue; var value= rows[i].getElementsByTagName("value")[0].childNodes[0].nodeValue; html = html + " text: " + text + " value: " +value; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.