NgxXmlToJson
An Angular 14 library to convert xml to json.
Installation
npm install ngx-xml-to-json --save
Example
import { Component } from '@angular/core';
import { NgxXmlToJsonService } from 'ngx-xml-to-json';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
xml= `<contact-info><address category = "Profile" type = "Address" ><name color='white'><![CDATA[ Welcome to XML Converter]]>XML Parser</name><company>OpenSource</company><phone>(011) 123-4567</phone></address><address/><personal category = "Name"> XML Parser</personal></contact-info>`;
constructor(private ngxXmlToJsonService: NgxXmlToJsonService) {
const options = { // set up the default options
textKey: 'text', // tag name for text nodes
attrKey: 'attr', // tag for attr groups
cdataKey: 'cdata', // tag for cdata nodes (ignored if mergeCDATA is true)
};
const jsonObj = this.ngxXmlToJsonService.xmlToJson(this.xml, options)
}
}
Result
{
"contact-info": {
"address":[
{
"attr" : {
"category": "Profile",
"type": "address"
},
"company": {
"text": "OpenSource"
},
"name": {
"attr" : {
"color": "white"
},
"cData": "Welcome to XML Converter",
"text": "XML Parser"
},
"phone": {
"text": "(011) 123-4567"
}
}
],
"personal": {
"attr" : {
"category": "Name"
},
"text": "XML Parser"
}
}
}