Do I need to parse my xml to get the data from the xml to render in the html? I'm currently getting a local .xml file using http.get
request and in console log its showing all the information that's in the xml file I think its just reading it.
Question is how to convert xml into json format in angular2?
I tried the console.log(data.title)
but this is returning me as unidentified
getXml() {
this.http.get('../../xmlConf/dashboard_journey.xml')
.map(response => response.text())
.subscribe(data => {
if(data) {
console.log('fetching your xml');
console.log(data); // this shows me everything
}
});
}
xml
<xml>
<journey>
<title>Click and Collect</title>
<shortDesc>
<short_desc_1>lorem</short_desc_1>
<short_desc_2>lorem</short_desc_2>
</shortDesc>
<longDesc>
lorem
</longDesc>
<imageName>
<img src="/journey_images/clickandcollect.jpg" alt="Click and Collect"/>
</imageName>
</journey>
Using the answer below
var parser = new DOMParser();
xmlData = parser.parseFromString(data, "application/xml");
console.log of the above code:
Currently doing for loop to iterate the parsed xml, however in my view its only giving me one object. ngFor only iterates objects that is in an array.
for (var i = 0; i <= newFormat.childNodes.length; i++) {
this.title = newFormat.getElementsByTagName('title')[i].childNodes[0].nodeValue;
this.desc = newFormat.getElementsByTagName('desc')[i].childNodes[0].nodeValue;
console.log(this.title + ' = ' + this.desc);
}
view:
<li>{{ title }}</li>
console.log(data[0].title)