简体   繁体   English

如何使用javascript将xml属性及其值提取为json格式

[英]How to extract xml attribute and its value into json format using javascript

Seek for help to transform the xml to json.寻求帮助将xml转换为json。 I got an input with this.我对此有意见。

"<logRecord>
  <logRecord>
    <logRecord>
      <class name="dto">
        <logField fieldName="ID" oldValue=" " newValue="650"/>
        <logField fieldName="submissionDt" oldValue="" newValue="03-12-2022"/>
      </class>
    </logRecord>
  </logRecord>
</logRecord>"

and i want change to我想改成

[{fieldName : 'ID', oldValue : '' , newValue " '650'}.
{fieldName : 'submissionDt', oldValue : '' , newValue : '03-12-2022'}]

Is it possible?可能吗? Thanks谢谢

I'm almost certain there is probably a better way of doing this, so I fully expect someone to come along and critique my attempt.我几乎可以肯定可能有更好的方法来做到这一点,所以我完全希望有人来批评我的尝试。 However, the way I would achieve this (using the functions I know) would be using xpath to extract given values from the XML.但是,我实现此目的的方法(使用我知道的函数)是使用 xpath 从 XML 中提取给定值。

I've commented the code to try explain exactly what I'm doing but in essence, I'm looping over each <class> then looping each <logField> before finally looping over each of the attributes.我对代码进行了评论,试图准确解释我在做什么,但本质上,我循环遍历每个<class> ,然后循环每个<logField> ,最后循环遍历每个属性。

 //First we'll take our (slightly more complex) XML input string var xmlString = "<logRecord><logRecord><logRecord><class name=\"dto\"><logField fieldName=\"ID\" oldValue=\"\" newValue=\"650\" /><logField fieldName=\"submissionDt\" oldValue=\"\" newValue=\"03-12-2022\" /></class></logRecord><logRecord><class name=\"dto\"><logField fieldName=\"ID\" oldValue=\"\" newValue=\"123\" /><logField fieldName=\"submissionDt\" oldValue=\"\" newValue=\"19-12-2022\" /></class></logRecord></logRecord></logRecord>"; var parser = new DOMParser(); var xmlDoc = parser.parseFromString(xmlString, "text/xml"); //Create a new XPathEvaluator const evaluator = new XPathEvaluator(); //Evaluate given xpath to target every <class> found inside the XML const classExpression = evaluator.createExpression('//class'); const classResult = classExpression.evaluate(xmlDoc, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); //Declare a final array var arr = []; //Loop each found instance of <class> for (i=0; i<classResult.snapshotLength; i++) { //Find all instances of <logField> inside of THIS <class> const logFieldExpression = evaluator.createExpression('./logField'); const logFieldResult = logFieldExpression.evaluate(classResult.snapshotItem(i), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); //Declare an array to hold each of our <logField>s var logFieldObj = []; //Loop each <logField> inside of the <class> for (j=0; j<logFieldResult.snapshotLength; j++) { //Find all attributes for THIS <logField> const attrEvaluator = evaluator.createExpression('./@*'); const attrResult = attrEvaluator.evaluate(logFieldResult.snapshotItem(j), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); //Declare a final object to hold each of the attributes fieldName, oldValue and newValue var attrObj = {}; //Loop over each attribute for (k=0; k<attrResult.snapshotLength; k++) { //Add attribute as attributeName => attributeValue to our object attrObj[attrResult.snapshotItem(k).localName] = attrResult.snapshotItem(k).textContent; } //Add our object to an array logFieldObj.push(attrObj); } //Create the final resulting array arr.push(logFieldObj); } console.log(arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM