I have get an xml file from a WebService
<MasterProducts> <MasterProduct> <Productcode>023DDC</Productcode> <Description>Dell CRT 17 Computer Monitor E771a</Description> <ThumbPic>NoImage.png</ThumbPic> <RRP>0.000000</RRP> <Stock>2</Stock> </MasterProduct> </MasterProducts>
The Chrome IDE I use automatically converts this to JSON, I’ve been trying to parse this without any success as I am quite new to Javascript.
The httpResponse returns an error variable and a data variable which holds the json file.
if(error===false)
{
if(data !== '')
{
objData = data.getElementsByTagName('MasterProducts');
//returns a NodeList here
var items = objData[0].getElementsByTagName('MasterProduct')[0].getElementsByTagName('Description')[0].firstChild.data;
}
else
{
alert("No Data");
}
}
I have only gone as far as getting the nodelist but I haven’t been able to get the data I need from the tags.
EDIT:
Using an online converter I’ve gotten the JSON below
{
"MasterProducts": {
"MasterProduct": {
"Productcode": "023DDC",
"Description": "Dell CRT 17 Computer Monitor E771a",
"ThumbPic": "NoImage.png",
"RRP": "0.000000",
"Stock": "2"
}
}
}
The JSON you’re getting back is valid, according to http://jsonlint.com/, so your script should be able to access the contents of data using dot notation, rather than having to parse nodes as with XML
description will contain “Dell CRT 17 Computer Monitor E771a”