easy-xml-parser

An ultimate tool to parse XMl int JSON format, with awesome result.

Usage no npm install needed!

<script type="module">
  import easyXmlParser from 'https://cdn.skypack.dev/easy-xml-parser';
</script>

README

Easy XML Parser

XML <-> JSON parser, for easy convert data avoiding unnecesary properties.

Examples


xmlToJson(`
  <root>
    <param arg1="some value"/>
    <param arg1="some value"/>
    <param arg1="some value"/>
    <param2>Text sample</param2>
    <param3 arg="123">Text sample</param3>
    <param4 arg="12" prop="123">
      <inner arg="123"/>
      <inner2 arg="123"/>
      <inner2 arg="123"/>
      <inner2 arg="123"/>
    </param4>
  </root>
`);

// will generate
{
  "root": {
    "param": [
      {
        "arg1": "some value"
      },
      {
        "arg1": "some value"
      },
      {
        "arg1": "some value"
      }
    ],
    "param2": "Text sample",
    "param3": "Text sample",
    "param4": {
      "arg": 12,
      "prop": 123,
      "inner": {
        "arg": 123
      },
      "inner2": [
        {
          "arg": 123
        },
        {
          "arg": 123
        },
        {
          "arg": 123
        }
      ]
    }
  }
}

jsonToXml({
  root: {
    param: [
      {
        arg1: "some value",
      },
      {
        arg1: "some value",
      },
      {
        arg1: "some value",
      },
    ],
    param2: "Text sample",
    param3: "Text sample",
    param4: {
      arg: 12,
      prop: 123,
      inner: {
        arg: 123,
      },
      inner2: [
        {
          arg: 123,
        },
        {
          arg: 123,
        },
        {
          arg: 123,
        },
      ],
    },
  },
});

// will generate
<root param2="Text sample" param3="Text sample">
  <param arg1="some value"/>
  <param arg1="some value"/>
  <param arg1="some value"/>
  <param4 arg="12" prop="123">
    <inner arg="123"/>
    <inner2 arg="123"/>
    <inner2 arg="123"/>
    <inner2 arg="123"/>
  </param4>
</root>

Important Bahavior

// Text content of XML tag will replace all attributes with

xmlToJson(`
  <root>
    <param arg1="some value">Text content</param>
  </root>
`)

// will generate
{
  root: {
    param: "Text content"
  }
}

// next example
xmlToJson(`
  <root>
    <param arg1="some value">Text content</param>
    <param arg1="some value">Text content</param>
    <param arg1="some value">Text content</param>
  </root>
`)

// will generate
{
  "root": {
    "param": [
      "Text content",
      "Text content",
      "Text content"
    ]
  }
}

// Single XMP tag will become as attribute and will replace exisiting one with the same name