Doug Lerner
Doug Lerner

Reputation: 1533

How to parse an xml string in javascript? (not client-side, browser-related)

I was wondering if anybody could point me to a Core (not client-side) JavaScript solution for parsing an xml string. For example, turning it into an object I could pluck properties out of. Or even an array.

I've searched high and low, and all the solutions I see are client-side solutions which make use of a particular browser's DOM. Please note, I am programming in JavaScript, but this is not a DOM-related question. It's a server-side calculation.

For example, I have this string:
<IndustryTerm count="2" relevance="0.553">food system</IndustryTerm><Person count="2" relevance="0.586">Alexandra Spieldoch</Person><Event count="1">Environmental Issue</Event><IndustryTerm count="1" relevance="0.154">rural finance</IndustryTerm><IndustryTerm count="1" relevance="0.335">food security</IndustryTerm><IndustryTerm count="1" relevance="0.280">e-consultation</IndustryTerm><IndustryTerm count="1" relevance="0.154">high-level food security dialogues</IndustryTerm><IndustryTerm count="1" relevance="0.154">food markets</IndustryTerm><Position count="1" relevance="0.335">representative</Position><SocialTags><SocialTag importance="2">Sociology<originalValue>Sociology</originalValue></SocialTag><SocialTag importance="2">Food security<originalValue>Food security</originalValue></SocialTag><SocialTag importance="1">Food politics<originalValue>Food politics</originalValue></SocialTag><SocialTag importance="1">Gender<originalValue>Gender</originalValue></SocialTag><SocialTag importance="1">Biology<originalValue>Biology</originalValue></SocialTag></SocialTags>

My goal is to find all the relevance and importance values, and the associated values between the tags. To be more specific, in this case:

<IndustryTerm count="1" relevance="0.154">food markets</IndustryTerm>

I'd like to know the tag name (IndustryTerm) the relevance (0.154) and the the value between the open and close tags (food markets).

I've been fiddling around with regular expressions, but haven't been able to "hit the spot" yet and was just wondering if there is something out there.

Again, this is not related to client-side JavaScript or browsers, but does need to be in standard, Core JavaScript.

doug

Upvotes: 0

Views: 5188

Answers (2)

Nikhil Dabas
Nikhil Dabas

Reputation: 2393

You might be able to use an alternative syntax for parsing XML that is supported by SpiderMonkey. I grabbed a copy of the SpiderMonkey JavaScript Shell from the Firefox nightly builds page, and stealing from siberian's example, this works:

js> options('allow_xml');
""
js> var person = new XML("<person><name>Bob Smith</name><likes><os>Linux</os><browser>Firefox</browser><language>JavaScript</language><language>Python</language></likes></person>");
js> person.name;
<name>Bob Smith</name>
js> person.name.text()
Bob Smith
js> person.likes.browser.text();
Firefox

Upvotes: 0

siberian
siberian

Reputation: 73

Core Javascript has built in XML methods, is this what you are looking for?

https://developer.mozilla.org/En/E4X/Processing_XML_with_E4X

var person = <person>
  <name>Bob Smith</name>
  <likes>
    <os>Linux</os>
    <browser>Firefox</browser>
    <language>JavaScript</language>
    <language>Python</language>
  </likes>
</person>;

alert(person.name); // Bob Smith
alert(person['name']); // Bob Smith
alert(person.likes.browser); // Firefox
alert(person['likes'].browser); // Firefox

The link has examples for working with lists, attributes etc.

Upvotes: 4

Related Questions