Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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

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.
 
Share this answer
 
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?
 
Share this answer
 
so, how exactly do i view the converted code?

Is this what you're saying?

XML
<head>
<script type="text/javascript" src="xml2json.js"></script>

XML
<?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>




C#
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.
 
Share this answer
 
Comments
[no name] 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.
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:
JavaScript
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900