I would like to display a XML tree structure as it is in the HTML page.
XML:
<employees>
<employee id="1">
<firstName>OneFirstName</firstName>
<lastname>OneSecondName</lastname>
</employee>
<employee id="2">
<firstname>TwoFirstName</firstname>
<lastname>TwoSecondName</lastname>
</employee>
</employees>
The string version of XML comes from a WCF service and rendered using the Angular binding such as below.
{{MyObj.XMLText}}
Few things I have tried and didn't work for me. May I am missing something and looking for the missing link.
http://tiku.io/questions/2496035/how-to-display-xml-data-in-div[
^]
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23513630.html[
^]
http://stackoverflow.com/questions/10498188/how-to-display-xml-data-in-div[
^]
The above links suggest two approaches displaying the XML:
1- HTML conversion using htmlEntities(...) which replaces characters such as <, > to their HTML version. Tried this and it shows XML, however indentation and spacing is lost. Everything comes in a line and not formatted as XML.
2. HTML conversion using vkbeautify.js library. Tried this and it gives proper formatting in the alert dialog. However, in the main page only XML tag values are displayed in a continuous fashion. Here indentation is lost.
Please find below the code. I have used simple notepad to create it.
NOTE: download vkbeautify.0.99.00.beta.js from
https://code.google.com/p/vkbeautify/downloads/list[
^]
Code: index.html
<html>
<title>
<h1> HTML Javascript page </h1>
</title>
<head>
<script type="text/javascript" src="vkbeautify.0.99.00.beta.js"></script>
</head>
<body>
<h1>Employees</h1>
<button type="button" onclick="displayXml()">Display Employee xml.</button>
<hr/>
<p id="empDetails"></p>
<script>
var xmlString = "<employees>";
xmlString = xmlString +" <employee id='1'>";
xmlString = xmlString +" <firstName>OneFirstName</firstName>";
xmlString = xmlString +" <lastname>OneSecondName</lastname>";
xmlString = xmlString +" </employee>";
xmlString = xmlString +" <employee id='2'>";
xmlString = xmlString +" <firstname>TwoFirstName</firstname>";
xmlString = xmlString +" <lastname>TwoSecondName</lastname>";
xmlString = xmlString +" </employee>";
xmlString = xmlString +"</employees>";
function displayXml() {
document.getElementById('empDetails').innerHTML = xmlString;
};
function htmlEntities(str) {
var htmlString = String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
return htmlString;
}
function displayXml() {
// Normal display of xml
document.getElementById('empDetails').innerHTML = xmlString;
alert("Normal display: \n" + xmlString);
// HTML conversion only
var htmlString = htmlEntities(xmlString);
document.getElementById('empDetails').innerHTML = htmlString;
alert("HTML formatted display: \n" + htmlString);
// Using vkbeautify
var beautifyHtmlString = vkbeautify.xml(xmlString);
document.getElementById('empDetails').innerHTML = beautifyHtmlString;
alert("VKbeautify display: \n" + beautifyHtmlString);
};
</script>
</body>
</html>
Thanks in advance!!!