Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 727
rzcl Avatar asked Mar 27 '12 22:03

rzcl


People also ask

Can we convert XML to string?

XML Stringify is easy to use tool to convert XML to XML String while escaping special characters. Copy, Paste and Stringify.

How do I read an XML file as a string?

In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.

Can you convert XML to HTML?

Open our free XML to HTML converter website. Click inside the file drop area to upload XML file or drag & drop XML file. Click on Convert button. Your XML files will be uploaded and converted to HTML result format.


1 Answers

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); 
like image 144
veblock Avatar answered Oct 06 '22 14:10

veblock