Skip to content

Instantly share code, notes, and snippets.

@demircancelebi
Forked from chinchang/xmlToJson.js
Last active September 1, 2023 12:40
Show Gist options
  • Save demircancelebi/f0a9c7e1f48be4ea91ca7ad81134459d to your computer and use it in GitHub Desktop.
Save demircancelebi/f0a9c7e1f48be4ea91ca7ad81134459d to your computer and use it in GitHub Desktop.
Function to convert XML to JSON
// Changes XML to JSON
// Modified version from here: http://davidwalsh.name/convert-xml-json
xmlToJson(xml) {
// Create the return object
let obj = {};
if (xml.nodeType === 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj['@attributes'] = {};
for (let j = 0; j < xml.attributes.length; j += 1) {
const attribute = xml.attributes.item(j);
obj['@attributes'][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType === 3) { // text
obj = xml.nodeValue;
}
// do children
// If just one text node inside
if (xml.hasChildNodes() && xml.childNodes.length === 1 && xml.childNodes[0].nodeType === 3) {
obj = xml.childNodes[0].nodeValue;
} else if (xml.hasChildNodes()) {
for (let i = 0; i < xml.childNodes.length; i += 1) {
const item = xml.childNodes.item(i);
const nodeName = item.nodeName;
if (typeof (obj[nodeName]) === 'undefined') {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof (obj[nodeName].push) === 'undefined') {
const old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
},
@mohancm
Copy link

mohancm commented Jun 5, 2018

how to use this?

@ragtag
Copy link

ragtag commented Feb 20, 2019

@mohancm

xml_as_string = "Your XML file as a string here"
dom = new DOMParser().parseFromString(xml_as_string, "text/xml");
json = xmlToJson(dom)

@ragtag
Copy link

ragtag commented Feb 20, 2019

How do you access the @attributes, when using dot notations?

var color = body.@attributes.bgcolor

I can only get this to work, if I remove the @ from your code, and just access it as body.attributes.bgcolor, otherwise I get an invalid symbol

I'm pretty new to JavaScript, so this may be something very basic I'm missing here.

@obisi7
Copy link

obisi7 commented Apr 24, 2019

Followup. I moved the xmlToJson into the main file, rather than importing it. Now I am getting an error on line 22 i.e.
"xml.haschildNodes () is not a function error. Please help me as I am at my wits end but continue to try and solve the issue myself as a way to learn . Thanks as you help a desperate beginning developer.

@obisi7
Copy link

obisi7 commented Apr 24, 2019

I added the below code snippet which corrected the xml.hasCHildNodes is not a function error,
DOMParser = require('xmldom').DOMParser; let result = new DOMParser().parseFromString(response, 'text/xml'); let resJson = this.xmlToJson(result);

Now I am getting this error on line 29: xmlToJson is not defined. I actually am not understanding the self reference of the called function defined within itself. Did I copy this wrong? Most importantly is help to resolve this issue which is where I am stuck now. I am trying but do need community help. Sorry for the bother and moving target of help I need as I resolve previous error .

@obisi7
Copy link

obisi7 commented Apr 24, 2019

I am getting closer. I decided to import my xmlTOJson function into the main file instead and revise the code as shown in the snippet below:
DOMParser = require('xmldom').DOMParser; let result = new DOMParser().parseFromString(response, 'text/xml'); let resJson = xmlToJson(result); console.log(resJson); this.setState({ data: resJson.rss.channel[0].item, isLoading: false, refreshing: false, error: response.error || null }); }) .catch(error => { this.setState({ error, isLoading: false }); // Alert.alert("Error", "Sorry, something went wrong. Please try again"); console.log('fetch', error) });

I now get the error : "Cannot read property of undefined" on line where setState() is called on data received. As shown in the output below, I am getting my xml converted to json. I am guessing I may not be referencing the elements correctly. Could anyone see my error and assist?

{undefined: Array(3), #text: " ↵", rss: {…}} #text: " ↵" rss: #text: (2) [" ↵ ↵", " ↵"] @attributes: {xmlns:content: "http://purl.org/rss/1.0/modules/content/", xmlns:wfw: "http://wellformedweb.org/CommentAPI/", xmlns:dc: "http://purl.org/dc/elements/1.1/", xmlns:atom: "http://www.w3.org/2005/Atom", xmlns:sy: "http://purl.org/rss/1.0/modules/syndication/", …} channel: #text: (19) [" ↵ ", " ↵ ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ ", " ↵ "] atom10:link: (2) [{…}, {…}] description: "Morgan State University" feedburner:info: {@attributes: {…}} generator: "https://wordpress.org/?v=5.1.1" item: Array(10) 0: {#text: Array(24), title: "New Leadership Appointments Bolster Morgan State University Division of Academic Affairs", link: "https://news.morgan.edu/new-leadership-appointments/", pubDate: "Wed, 24 Apr 2019 14:41:13 +0000", dc:creator: {…}, …} 1: {#text: Array(18), title: "Morgan SA+P Students Win 2019 Sustainable Growth Challenge", link: "https://news.morgan.edu/sustainable-growth-challenge/", pubDate: "Fri, 19 Apr 2019 17:53:46 +0000", dc:creator: {…}, …} 2: {#text: Array(19), title: "$5-Million Grant from NIST Benefits Morgan Students and Faculty", link: "https://news.morgan.edu/nist-grant/", pubDate: "Mon, 15 Apr 2019 21:22:19 +0000", dc:creator: {…}, …} 3: {#text: Array(12), title: "Morgan State University plotting new research, degrees around industrial hemp", link: "https://news.morgan.edu/morgan-state-university-pl…ting-new-research-degrees-around-industrial-hemp/", pubDate: "Fri, 12 Apr 2019 16:39:00 +0000", dc:creator: {…}, …} 4: {#text: Array(17), title: "Morgan Students Explore Afro-Cuban Culture in Havana", link: "https://news.morgan.edu/afro-cuban-culture/", pubDate: "Wed, 10 Apr 2019 21:07:13 +0000", dc:creator: {…}, …} 5: {#text: Array(17), title: "Morgan State University Names New Dean for School of Education and Urban Studies", link: "https://news.morgan.edu/new-seus-dean/", pubDate: "Mon, 08 Apr 2019 18:40:22 +0000", dc:creator: {…}, …} 6: {#text: Array(14), title: "Morgan President David Wilson Appointed to NCAA Board of Governors", link: "https://news.morgan.edu/morgan-president-david-wilson-appointed-to-ncaa-board-of-governors/", pubDate: "Mon, 01 Apr 2019 20:58:26 +0000", dc:creator: {…}, …} 7: {#text: Array(20), title: "Meet Morgan’s Moguls in the Making", link: "https://news.morgan.edu/meet-morgans-moguls-in-the-making/", pubDate: "Fri, 29 Mar 2019 21:41:00 +0000", dc:creator: {…}, …} 8: {#text: Array(22), title: "Congressman Elijah E. Cummings to Deliver Keynote at Morgan’s Spring Commencement", link: "https://news.morgan.edu/elijah-cummings-keynote/", pubDate: "Wed, 27 Mar 2019 14:29:49 +0000", dc:creator: {…}, …} 9: {#text: Array(19), title: "Morgan Professor Receives Innovation Award for Research in STEM", link: "https://news.morgan.edu/beya-honors-professor/", pubDate: "Fri, 15 Mar 2019 13:30:32 +0000", dc:creator: {…}, …} length: 10 __proto__: Array(0) language: "en-US" lastBuildDate: " ↵ Wed, 24 Apr 2019 15:44:43 +0000 " link: "https://news.morgan.edu" sy:updateFrequency: " ↵ 1 " sy:updatePeriod: " ↵ hourly " title: "News – Morgan State University Newsroom" __proto__: Object __proto__: Object undefined: (3) [{…}, {…}, {…}]

Thanks

@obisi7
Copy link

obisi7 commented Apr 24, 2019

I am sorry for the messy code. In the absence of an image tag for a scrrenshot, I copied the output into the code segment hoping it will format same correctly.

I think I am getting closer to the solution. I revised the setState() as: data: resJson.rss.channel.item and my data now came in correctly. However, I am missing the news image thumbnails. I will let the community know how I resolve this if someone doesn't;t see the error of my ways sooner. Thanks

@obisi7
Copy link

obisi7 commented Apr 25, 2019

Great News! I finally solved my own problem. Everything is working now. I had to include another child node to be retrieved (CDATA-Section) of type level 4 and bingo, all data needed came in and I can now extract what I need in the format I need it. Thanks all.

@xuxucode
Copy link

Another version:

// xml is the result of `DOMParser.parseFromString()`
function xml2json(xml) {
  if (xml.children.length === 0)
    return xml.textContent.trim()

  const obj = {}

  for (let child of xml.children) {
    const name = child.nodeName
    if (obj[name] === undefined)
      obj[name] = xml2json(child)
    else {
      if (!Array.isArray(obj[name]))
        obj[name] = [obj[name]]
      obj[name].push(xml2json(child))
    }
  }

  return obj
}

@victor123
Copy link

Great.Thanks much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment