本文介绍了XML到JSON的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



所以我正在运行这个名为MTConnect的界面。每个接口都有一个由不同服务器托管的唯一代理。界面吐出这样的XML代码



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

<pre lang="xml"><?xml version="1.0" encoding="UTF-8" ?><br />
- <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"><br />
  <Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="1" bufferSize="5000" /><br />
- <Devices><br />
- <Device sampleRate="10.0" name="MTConnectDevice" iso841Class="1" uuid="imts-demonstration" id="id1001"><br />
  <Description manufacturer="Company Name" serialNumber="Serial Number" /><br />
- <DataItems><br />
  <DataItem category="EVENT" id="id1" name="avail" type="AVAILABILITY" /><br />
  </DataItems><br />
- <Components><br />
- <Axes name="Axes" id="id105"><br />
- <DataItems><br />
  <DataItem type="PATH_FEEDRATE" name="path_feedratefrt" category="SAMPLE" id="id2" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
- <Components><br />
- <Rotary name="C" id="c"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id3" name="Srpm" type="SPINDLE_SPEED" units="REVOLUTION/MINUTE" subType="ACTUAL" /><br />
  </DataItems><br />
  </Rotary><br />
- <Linear name="X" id="x"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id4" name="Xabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" /><br />
  <DataItem category="SAMPLE" id="id5" name="Xfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
  </Linear><br />
- <Linear name="Y" id="y"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id6" name="Yabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" /><br />
  <DataItem category="SAMPLE" id="id7" name="Yfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
  </Linear><br />
- <Linear name="Z" id="z"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id8" name="Zabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" /><br />
  <DataItem category="SAMPLE" id="id9" name="Zfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
  </Linear><br />
  </Components><br />
  </Axes><br />
- <Controller name="Controller" id="id106"><br />
- <DataItems><br />
  <DataItem category="EVENT" id="id10" name="block" type="BLOCK" /><br />
  <DataItem category="EVENT" id="id11" name="execution" type="EXECUTION" /><br />
  <DataItem category="EVENT" id="id12" name="mode" type="CONTROLLER_MODE" /><br />
  <DataItem category="EVENT" id="id13" name="program" type="PROGRAM" /><br />
  <DataItem category="EVENT" id="id14" name="line" type="LINE" /><br />
  <DataItem category="CONDITION" id="id16" name="communications" type="COMMUNICATIONS" /><br />
  <DataItem category="CONDITION" id="id17" name="temperature" type="TEMPERATURE" /><br />
  </DataItems><br />
  </Controller><br />
  </Components><br />
  </Device><br />
  </Devices><br />
  </MTConnectDevices></pre><br />





但是,我需要将此代码转换为JSON。我可以向代理添加元素,它将每10秒左右更新一次,这意味着代码每10秒左右就会更改一次。我已经查找了几天不同的脚本,我找不到一个会在10秒的间隔内将XML转换为JSON的脚本。我永远不会使用XML和Javascript,所以我不确定要进入哪个方向。当我使用Javascript搜索XML to JSON时,我几乎点击了google中的每个链接,但它们似乎都没有帮助。



任何帮助表示赞赏。谢谢!



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!

推荐答案

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.


<pre lang="xml"><head><br />
<script type="text/javascript" src="xml2json.js"></script></pre><br />
<pre lang="xml"><?xml version="1.0" encoding="UTF-8" ?><br />
- <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"><br />
  <Header version="1.1" sender="Company MTConnect Instance" creationTime="2010-08-26T19:00:47-07:00" instanceId="1" bufferSize="5000" /><br />
- <Devices><br />
- <Device sampleRate="10.0" name="MTConnectDevice" iso841Class="1" uuid="imts-demonstration" id="id1001"><br />
  <Description manufacturer="Company Name" serialNumber="Serial Number" /><br />
- <DataItems><br />
  <DataItem category="EVENT" id="id1" name="avail" type="AVAILABILITY" /><br />
  </DataItems><br />
- <Components><br />
- <Axes name="Axes" id="id105"><br />
- <DataItems><br />
  <DataItem type="PATH_FEEDRATE" name="path_feedratefrt" category="SAMPLE" id="id2" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
- <Components><br />
- <Rotary name="C" id="c"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id3" name="Srpm" type="SPINDLE_SPEED" units="REVOLUTION/MINUTE" subType="ACTUAL" /><br />
  </DataItems><br />
  </Rotary><br />
- <Linear name="X" id="x"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id4" name="Xabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" /><br />
  <DataItem category="SAMPLE" id="id5" name="Xfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
  </Linear><br />
- <Linear name="Y" id="y"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id6" name="Yabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" /><br />
  <DataItem category="SAMPLE" id="id7" name="Yfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
  </Linear><br />
- <Linear name="Z" id="z"><br />
- <DataItems><br />
  <DataItem category="SAMPLE" id="id8" name="Zabs" type="POSITION" units="MILLIMETER" subType="ACTUAL" /><br />
  <DataItem category="SAMPLE" id="id9" name="Zfrt" type="AXIS_FEEDRATE" units="MILLIMETER/SECOND" subType="ACTUAL" /><br />
  </DataItems><br />
  </Linear><br />
  </Components><br />
  </Axes><br />
- <Controller name="Controller" id="id106"><br />
- <DataItems><br />
  <DataItem category="EVENT" id="id10" name="block" type="BLOCK" /><br />
  <DataItem category="EVENT" id="id11" name="execution" type="EXECUTION" /><br />
  <DataItem category="EVENT" id="id12" name="mode" type="CONTROLLER_MODE" /><br />
  <DataItem category="EVENT" id="id13" name="program" type="PROGRAM" /><br />
  <DataItem category="EVENT" id="id14" name="line" type="LINE" /><br />
  <DataItem category="CONDITION" id="id16" name="communications" type="COMMUNICATIONS" /><br />
  <DataItem category="CONDITION" id="id17" name="temperature" type="TEMPERATURE" /><br />
  </DataItems><br />
  </Controller><br />
  </Components><br />
  </Device><br />
  </Devices><br />
  </MTConnectDevices></pre><br />
<br />
<br />
<br />
<pre lang="cs">function update_json()<br />
{<br />
  // A simple call - myXML is a string containing your XML<br />
  myJsonObject=xml2json.parser(myXML);<br />
  // Do something with myJsonObject<br />
  // ...<br />
<br />
  // call this function again, with a ten second delay<br />
  window.setTimeout(function(){update_json();}, 10000);<br />
}</pre><br />
<br />
<br />
<br />
<br />
<br />





对不起,就像我说我是新来的javascript所以我只是想了解一下。



Sorry, like I said I'm new to javascript so I'm just trying to grasp an understanding.



这篇关于XML到JSON的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:02