rzcl
rzcl

Reputation: 323

Convert XML to String and append to page

I want to convert an xml element like this:

<asin>​B0013FRNKG​</asin>​

to string in javascript

I used XMLSerializer:

new XMLSerializer().serializeToString(xml);

the string only shows on alert() and in the console. On the page it just says

[object Element][object Element]

I want to get the string.

Upvotes: 32

Views: 118243

Answers (6)

Jose Kj
Jose Kj

Reputation: 2962

follow this to print,append data from xml data stored as string inside javscript

txt="<papers>"+"<paper>"+
 "<author>athor name</author>"+
"<title>title</title>"+
"<path>path</path>"+
"<track>which tack</track>"+
 "</paper>"+
"<paper>"+
"<author>athor name</author>"+
"<title>title</title>"+
"<path>path</path>"+
"<track>which tack</track>"+
"</paper>"+
"<paper>"+
"<author>athor name</author>"+
"<title>title</title>"+
"<path>path</path>"+
"<track>which tack</track>"+
"</paper>"+
"<papers>";
if (window.DOMParser)
  {
      parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");

   }
   else // Internet Explorer
    {
     xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
     xmlDoc.async=false;
     xmlDoc.loadXML(txt);
    }

x=xmlDoc.getElementsByTagName("paper"); 
for (var i = 0; i < x.length; i++) {  
    var athor =x[i].childNodes[0].firstChild.nodeValue;
    var title = x[i].childNodes[1].firstChild.nodeValue;
    var path = x[i].childNodes[2].firstChild.nodeValue;
    var tack =x[i].childNodes[3].firstChild.nodeValue;
    //do something with these values...
    //each iteration gives one paper details    
    var xml=document.getElementById("element_id");//<div id="element_id"></div>
    var li = document.createElement("br");// create a new <br>  
    newlink = document.createElement('A'); // creating an <a> element
    newlink.innerHTML = athor;// adding <a>athor value here</a>
    newlink.setAttribute('href', path);// <a href="path"></a>

    newlink.appendChild(li);// <a href="path">athor</a><br>
    document.getElementById("element_id").appendChild(newlink);//finaly it becomes <div id="element_id"><a href="path">athor</a><br></div>


}

Upvotes: 0

veblock
veblock

Reputation: 1924

You haven't told us how you go about displaying that object. XMLSerializer works on DOM nodes, so your object has to be added somewhere, for example:

document.getElementById('SomeDiv').appendChild(xml); 

and if you just want the full xml string to be displayed:

var xmlText = new XMLSerializer().serializeToString(xml);
var xmlTextNode = document.createTextNode(xmlText);
var parentDiv = document.getElementById('SomeDiv');
parentDiv.appendChild(xmlTextNode);

Upvotes: 54

Avinash Anand
Avinash Anand

Reputation: 695

<script type='text/javascript'>

    function xmlToString(xmlData) { 

        var xmlString;
        //IE
        if (window.ActiveXObject){
            xmlString = xmlData.xml;
        }
        // code for Mozilla, Firefox, Opera, etc.
        else{
            xmlString = (new XMLSerializer()).serializeToString(xmlData);
        }
        return xmlString;
    }   

</script>    

use this in case of IE for browser compatibility issues.

Upvotes: 7

maerics
maerics

Reputation: 156444

function getXmlString(xml) {
  if (window.ActiveXObject) { return xml.xml; }
  return new XMLSerializer().serializeToString(xml);
}
alert(getXmlString(xml));

Upvotes: 4

Anurag
Anurag

Reputation: 141879

If the DOM element <asin>​B0013FRNKG​</asin>​ is stored in the object element, then you can access the value using:

element.textContent

Upvotes: 0

inhan
inhan

Reputation: 7470

Did you try enclosing the result like in…

(new XMLSerializer()).serializeToString(xml)

Also, I'd use console instead to see the content better:

console.log((new XMLSerializer()).serializeToString(xml));

Upvotes: 3

Related Questions