Handle XML attributes when converting XML to JSON using Node.JS xml2js

thotheolh

I am trying to convert an XML file into JSON using xml2js on Node.JS.

When I hit an attribute, it will give a '_' and '$' characters as replacement.

I am fully aware that JSON does not have the concept of attributes that XML does.

How do I convert the following XML document:

<id>
  <name language="en">Bob</name>
  <name>Alice</name>
</id>

Into a JSON format something like:

{
    "id": {
        "name": [{
            "language": "en",
            "text": "bob"
        }, "alice"]
    }
}

My code in Node.JS is:

const fs = require('fs');
const util = require('util');
const json = require('json');
const xml2js = require('xml2js');

const xml = fs.readFileSync('./test.xml', 'utf-8', (err, data) => {
  if (err) throw err;  
});

const jsonStr = xml2js.parseString(xml, function (err, result) {
  if (err) throw err;
    console.log(util.inspect(JSON.parse(JSON.stringify(result)), { depth: null }));
});

The current output is:

{ id: { name: [ { _: 'Bob', '$': { language: 'en' } }, 'Alice' ] } }
Jerome Diaz

will output

{
  id: { name: [ { language: 'en', text: 'Bob' }, { text: 'Alice' } ] }
}

the code:

const fs = require('fs');
const util = require('util');
const json = require('json');
const xml2js = require('xml2js');

const xml = fs.readFileSync('./test.xml', 'utf-8', (err, data) => {
  if (err) throw err;  
});

const jsonStr = xml2js.parseString(xml, function (err, result) {

  const nameArray = result.id.name;

  const newNameArray = nameArray.map(nameValue => {
    let text = '';
    let attributes = {};
    if (typeof nameValue === 'string') {
      text = nameValue
    } else if (typeof nameValue === 'object') {
      text = nameValue['_']
      attributes = nameValue['$']
    }
    return {
      ...attributes,
      text
    }
  })

  const newResult = {
    id: {
      name: newNameArray
    }
  }

  if (err) throw err;
    console.log(util.inspect(JSON.parse(JSON.stringify(newResult)), { depth: null }));
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

When converting xml to json , several objects name change to "@attributes"

分類Dev

xml2jsは解析されたJSONを返しません

分類Dev

XMLをJSONに解析して元に戻します... xml2jsとxmlbuilderを使用しますか?

分類Dev

node.jsルート内の変数にxml2js応答を渡す方法は?

分類Dev

How to format and truncate numbers when converting XML to XLS (using XSL)

分類Dev

Node.js-Xml解析

分類Dev

Node.js - XML validation

分類Dev

How to write a stylesheet for Xml nested node with attributes?

分類Dev

xml2jsを使用してAngular6で未定義のparseString

分類Dev

xml2js:パーサーを関数に入れます

分類Dev

xml2js を使用した解析に関する問題

分類Dev

xml2jsでXMLタグを削除および更新する方法

分類Dev

Node.js won't let me install the xml2json module

分類Dev

Angular2でJSライブラリxml2jsを使用する

分類Dev

xml2jsで解析されたjsonのアイテムをループするときにxml属性にアクセスできません

分類Dev

Convert XML attributes to elements using XSLT

分類Dev

MarkLogic: Converting an XML document into an XML string using XQuery

分類Dev

Node.js-XML検証

分類Dev

create xml with attributes programmatically

分類Dev

xml2js解析-メタデータ属性値を抽出する方法は?

分類Dev

TSQL Converting Characters in XML

分類Dev

Is it possible to use attributes when accessing XML nodes using dot-notation?

分類Dev

Node xml2jsモジュールを使用して文字をエスケープするにはどうすればよいですか?

分類Dev

Node.JSxml2jsを使用してXMLをJSONに変換するときにXML属性を処理する

分類Dev

Node.js:xml-js(npm ver 1.6.4)

分類Dev

Dynamically generated buttons from XML always have the same attributes of the last node in XML file

分類Dev

conversion failed when converting date and/or time from character string in xml

分類Dev

Add Xml string to an Xml Node

分類Dev

Using xmlStar to get XML node attribute

Related 関連記事

  1. 1

    When converting xml to json , several objects name change to "@attributes"

  2. 2

    xml2jsは解析されたJSONを返しません

  3. 3

    XMLをJSONに解析して元に戻します... xml2jsとxmlbuilderを使用しますか?

  4. 4

    node.jsルート内の変数にxml2js応答を渡す方法は?

  5. 5

    How to format and truncate numbers when converting XML to XLS (using XSL)

  6. 6

    Node.js-Xml解析

  7. 7

    Node.js - XML validation

  8. 8

    How to write a stylesheet for Xml nested node with attributes?

  9. 9

    xml2jsを使用してAngular6で未定義のparseString

  10. 10

    xml2js:パーサーを関数に入れます

  11. 11

    xml2js を使用した解析に関する問題

  12. 12

    xml2jsでXMLタグを削除および更新する方法

  13. 13

    Node.js won't let me install the xml2json module

  14. 14

    Angular2でJSライブラリxml2jsを使用する

  15. 15

    xml2jsで解析されたjsonのアイテムをループするときにxml属性にアクセスできません

  16. 16

    Convert XML attributes to elements using XSLT

  17. 17

    MarkLogic: Converting an XML document into an XML string using XQuery

  18. 18

    Node.js-XML検証

  19. 19

    create xml with attributes programmatically

  20. 20

    xml2js解析-メタデータ属性値を抽出する方法は?

  21. 21

    TSQL Converting Characters in XML

  22. 22

    Is it possible to use attributes when accessing XML nodes using dot-notation?

  23. 23

    Node xml2jsモジュールを使用して文字をエスケープするにはどうすればよいですか?

  24. 24

    Node.JSxml2jsを使用してXMLをJSONに変換するときにXML属性を処理する

  25. 25

    Node.js:xml-js(npm ver 1.6.4)

  26. 26

    Dynamically generated buttons from XML always have the same attributes of the last node in XML file

  27. 27

    conversion failed when converting date and/or time from character string in xml

  28. 28

    Add Xml string to an Xml Node

  29. 29

    Using xmlStar to get XML node attribute

ホットタグ

アーカイブ