PLEASE DO NOT COMMENT SAYING YOU CANNOT PARSE XML WITH REGEX, IT CAN BE DONE IT'S JUST NOT THE BEST WAY. AND PLEASE DON'T NEGATIVE THIS QUESTION FOR NO REASON.
On Parse.com's cloud code you currently cannot convert XML to json without major coding effort. I found the following code at: http://killzonekid.com/worlds-smallest-fastest-xml-to-json-javascript-converter/
xml = xml.replace(/s/g, ' ').replace(/< *?[^>]*?? *>/g, '').replace(/< *!--[^>]*?-- *>/g, '').replace(/< *(/?) *(w+):(w+)/g, '<$1$2_$3').replace(/< *(w+)([^>]*?)/ *>/g, '< $1$2>').replace(/(w+):(w+) *= *"([^>]*?)"/g, '$1_$2="$3"').replace(/< *(w+)((?: *w+ *= *" *[^"]*?")+ *)>( *[^< ]*?.*?)< */ *1 *>/g, '< $1$2 value="$3">').replace(/ *(w+) *= *"([^>]*?)" */g, '< $1>$2').replace(/< *(w+) *</g, '<$1>< ').replace(/> *>/g, '>').replace(/< */ *(w+) *> *< *1 *>/g, '').replace(/"/g, '"').replace(/< *(w+) *>([^<>]*?)< */ *1 *>/g, '"$1":"$2",').replace(/< *(w+) *>([^<>]*?)< */ *1 *>/g, '"$1":{$2},').replace(/< *(w+) *>(?=.*?< /1},{)/g, '"$1":[{').split(/},{/).reverse().join('},{').replace(/< */ *(w+) *>(?=.*?"1":[{)/g, '}],').split(/},{/).reverse().join('},{').replace(/< /(w+)},{1>/g, '},{').replace(/< *(w+)[^>]*?>/g, '"$1":{').replace(/< */ *w+ *>/g,'},').replace(/} *,(?= *(}|]))/g, '}').replace(/] *,(?= *(}|]))/g, ']').replace(/" *,(?= *(}|]))/g, '"').replace(/ *, *$/g, '');
It actually does quite a good job of converting XML to json.
There are a few querks with the code.
1. It messes up the attributes.
- It doesn't like names with hyphens in them.
To fix the hyphens I changed all the w+ to w[w'-] Is this the best way?
Here is an example XML document
<?xml version="1.0" encoding="UTF-8" ?>
<api>
<products total-matched="1618" records-returned="1" page-number="1">
<product>
<ad-id>1234</ad-id>
<supplier-name>Window World</supplier-name>
<supplier-category>3703703</supplier-category>
<buy-url>http://website.com</buy-url>
<currency>USD</currency>
<description>Window</description>
<image-url>http://website.com/windowa/80x80.jpg</image-url>
<in-stock>yes</in-stock>
<manufacturer-name>Window World</manufacturer-name>
<name>Half Pain Glass</name>
<price>31.95</price>
<retail-price>87.60</retail-price>
<sale-price>29.95</sale-price>
<sku>5938</sku>
<upc></upc>
</product>
</products>
</api>
Example output:
{
"api": {
"products": {
"total-matched": {
1618 "records-returned": {
1 "page-number": {
1 >
"product": {
"adid": "1234",
"suppliername": "Window World",
"suppliercategory": "3703703",
"buyurl": "http://website.com",
"currency": "USD",
"description": "Window",
"imageurl": "http://website.com/windowa/80x80.jpg",
"instock": "yes",
"manufacturername": "Window World",
"name": "Half Pain Glass",
"price": "31.95",
"retailprice": "87.60",
"saleprice": "29.95",
"sku": "5938",
"upc": ""
}
}
}
}
}
}
}
See Question&Answers more detail:
os