Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML JSON
Hey guys,
 
So I'm running this interface called MTConnect. Each interface has a unique agent that is hosted by different servers. The interface spits out XML code like this
 
<?xml version="1.0" encoding="UTF-8" ?>
- <MTConnectDevices xmlns="urn:mtconnect.org:MTConnectDevices:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1 http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
  <Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="1" bufferSize="5000" />
- <Devices>
- <Device sampleRate="10.0" name="MTConnectDevice" iso841Class="1" uuid="imts-demonstration" id="id1001">
  <Description manufacturer="Company Name" serialNumber="Serial Number" />
- <DataItems>
  <DataItem category="EVENT" id="id1" name="avail" type="AVAILABILITY" />
  </DataItems>
- <Components>
- <Axes name="Axes" id="id105">
- <DataItems>
  <DataItem type="PATH_FEEDRATE" name="path_feedratefrt" category="SAMPLE" id="id2" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
- <Components>
- <Rotary name="C" id="c">
- <DataItems>
  <DataItem category="SAMPLE" id="id3" name="Srpm" type="SPINDLE_SPEED" units="REVOLUTION/MINUTE" subType="ACTUAL" />
  </DataItems>
  </Rotary>
- <Linear name="X" id="x">
- <DataItems>
  <DataItem category="SAMPLE" id="id4" name="Xabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" />
  <DataItem category="SAMPLE" id="id5" name="Xfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
  </Linear>
- <Linear name="Y" id="y">
- <DataItems>
  <DataItem category="SAMPLE" id="id6" name="Yabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" />
  <DataItem category="SAMPLE" id="id7" name="Yfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
  </Linear>
- <Linear name="Z" id="z">
- <DataItems>
  <DataItem category="SAMPLE" id="id8" name="Zabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" />
  <DataItem category="SAMPLE" id="id9" name="Zfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
  </Linear>
  </Components>
  </Axes>
- <Controller name="Controller" id="id106">
- <DataItems>
  <DataItem category="EVENT" id="id10" name="block" type="BLOCK" />
  <DataItem category="EVENT" id="id11" name="execution" type="EXECUTION" />
  <DataItem category="EVENT" id="id12" name="mode" type="CONTROLLER_MODE" />
  <DataItem category="EVENT" id="id13" name="program" type="PROGRAM" />
  <DataItem category="EVENT" id="id14" name="line" type="LINE" />
  <DataItem category="CONDITION" id="id16" name="communications" type="COMMUNICATIONS" />
  <DataItem category="CONDITION" id="id17" name="temperature" type="TEMPERATURE" />
  </DataItems>
  </Controller>
  </Components>
  </Device>
  </Devices>
  </MTConnectDevices>
 
However, I need to convert this code to JSON. I can add elements to the agent and it will update every 10 seconds or so, which means the code will change every 10 seconds or so. I have been looking up different scripts for days and I can't find one that will convert XML to JSON within intervals like 10 seconds. I'm never to XML and Javascript, so I'm not really sure what direction to go in. I've clicked on almost every link in google when I search "XML to JSON using Javascript" but none of them seem to help.
 
any help is appreciated. thanks!
Posted 27-Jun-11 10:17am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When working with intervals you can use window.setTimeout(code,millisec,lang) [^].
 
For a converter script take a look here:
http://www.thomasfrank.se/xml_to_json.html[^]
 
Download the javascript file (xml2json.js) and add a link in the head of the page. I didn't test the script, so there may be better, faster scripts. But this one is easy to include in your code.
 
Putting the timeout and conversion together, create a function like this:
function update_json()
{
  // A simple call - myXML is a string containing your XML
  myJsonObject=xml2json.parser(myXML);
  // Do something with myJsonObject
  // ...

  // call this function again, with a ten second delay
  window.setTimeout(function(){update_json();}, 10000);
}
 
Please note that this code will call the converter script every ten seconds + the time it needs to convert the code.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

so, how exactly do i view the converted code?
 
Is this what you're saying?
 
<head>
<script type="text/javascript" src="xml2json.js"></script>
<?xml version="1.0" encoding="UTF-8" ?>
- <MTConnectDevices xmlns="urn:mtconnect.org:MTConnectDevices:1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:1.1 http://www.mtconnect.org/schemas/MTConnectDevices_1.1.xsd">
  <Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="1" bufferSize="5000" />
- <Devices>
- <Device sampleRate="10.0" name="MTConnectDevice" iso841Class="1" uuid="imts-demonstration" id="id1001">
  <Description manufacturer="Company Name" serialNumber="Serial Number" />
- <DataItems>
  <DataItem category="EVENT" id="id1" name="avail" type="AVAILABILITY" />
  </DataItems>
- <Components>
- <Axes name="Axes" id="id105">
- <DataItems>
  <DataItem type="PATH_FEEDRATE" name="path_feedratefrt" category="SAMPLE" id="id2" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
- <Components>
- <Rotary name="C" id="c">
- <DataItems>
  <DataItem category="SAMPLE" id="id3" name="Srpm" type="SPINDLE_SPEED" units="REVOLUTION/MINUTE" subType="ACTUAL" />
  </DataItems>
  </Rotary>
- <Linear name="X" id="x">
- <DataItems>
  <DataItem category="SAMPLE" id="id4" name="Xabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" />
  <DataItem category="SAMPLE" id="id5" name="Xfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
  </Linear>
- <Linear name="Y" id="y">
- <DataItems>
  <DataItem category="SAMPLE" id="id6" name="Yabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" />
  <DataItem category="SAMPLE" id="id7" name="Yfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
  </Linear>
- <Linear name="Z" id="z">
- <DataItems>
  <DataItem category="SAMPLE" id="id8" name="Zabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" />
  <DataItem category="SAMPLE" id="id9" name="Zfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" />
  </DataItems>
  </Linear>
  </Components>
  </Axes>
- <Controller name="Controller" id="id106">
- <DataItems>
  <DataItem category="EVENT" id="id10" name="block" type="BLOCK" />
  <DataItem category="EVENT" id="id11" name="execution" type="EXECUTION" />
  <DataItem category="EVENT" id="id12" name="mode" type="CONTROLLER_MODE" />
  <DataItem category="EVENT" id="id13" name="program" type="PROGRAM" />
  <DataItem category="EVENT" id="id14" name="line" type="LINE" />
  <DataItem category="CONDITION" id="id16" name="communications" type="COMMUNICATIONS" />
  <DataItem category="CONDITION" id="id17" name="temperature" type="TEMPERATURE" />
  </DataItems>
  </Controller>
  </Components>
  </Device>
  </Devices>
  </MTConnectDevices>
 

 
function update_json()
{
  // A simple call - myXML is a string containing your XML
  myJsonObject=xml2json.parser(myXML);
  // Do something with myJsonObject
  // ...

  // call this function again, with a ten second delay
  window.setTimeout(function(){update_json();}, 10000);
}
 
Sorry, like I said I'm new to javascript so I'm just trying to grasp an understanding.
  Permalink  
Comments
Ruard at 29-Jun-11 3:03am
   
Yes, add a link to the script in the head of the html document. This is simple html. Make sure the file (xml2json.js) exists.
 
With javascript you can put your xml into a string (myXML) and call 'xml2json.parser(myXML)' to convert the xml to json.
 
In this case the function needs to be called with intervals so you use the window.setTimeout function, which allows the client to run scripts with an interval. Since there is no timer object you need to call the same function again to simulate a timer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

What I don't understand is how the script gets a hold of my XML. You said put it into a string...but where and how am I putting it into a string?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

you literally set it to a var in your JS code (inside a script tag on your html page) copy and paste the XML there between quotes, in other words a string assignment.
 
But, a much better way would be to use AJAX to read an XML file on disk into a string variable then pass that to the xml2json.parser() method. Google "jquery read file local disk" for an example --> if you use this technique, you'll have to include jquery-x.x.x.js or jquery-x.x.x.min.js (where x represents the version) in a separate script tag above the script tag with your code/use of xml2json.parse(). JS also supplies inherent AJAX functionality to read from disk, but jQuery makes it much easier AND cross-browser.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 275
1 OriginalGriff 230
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 31 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100