Skip to content

pedroentringer/json2xml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Pedro EntringerPedro Entringer
Pedro Entringer
and
Pedro Entringer
Sep 14, 2020
51f3ad7 · Sep 14, 2020

History

11 Commits
Sep 14, 2020
Jul 3, 2020
Jul 3, 2020
Jul 3, 2020
Jul 3, 2020
Jul 3, 2020
Sep 14, 2020
Jul 3, 2020
Jul 3, 2020
Jul 3, 2020

Repository files navigation

JSON2XML

Convert JSON to XML

Simple Convert

const {json2xml} = require('@pedroentringer/json2xml')

const json = { people: {name: "Pedro", age: 'XX' } }

const xml = json2xml(json)

Result:

<?xml version='1.0' encoding='utf-8'?>
<people>
  <name>Pedro</name>
  <age>XX</age>
</people>

Simple Convert With Attributes

const {json2xml} = require('@pedroentringer/json2xml')

const json = { 
  people: { 
    name: {
      attrs: {
        encode: 'utf8',
        isFullName: true
      },
      value: 'Pedro Entringer'
    }, 
    age: 'XX'
  }
}

const xml = json2xml(json)

Result:

<?xml version='1.0' encoding='utf-8'?>
<people>
    <name encode="utf8" isFullName="true" >Pedro Entringer</name>
    <age>XX</age>
</people>

Object Convert

const {json2xml} = require('@pedroentringer/json2xml')

const json = { 
  people: { 
    name: {
      attrs: {
        encode: 'utf8',
        isFullName: true
      },
      value: 'Pedro Entringer'
    }, 
    age: 'XX',
    mother: {
      name: {
        attrs: {
          encode: 'utf8',
          isFullName: true
        },
        value: 'Juliana Entringer'
      },
      age: 'XX'
    }
  }
}

const xml = json2xml(json)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<people>
   <name encode="utf8" isFullName="true">Pedro Entringer</name>
   <age>XX</age>
   <mother>
      <name encode="utf8" isFullName="true">Juliana Entringer</name>
      <age>XX</age>
   </mother>
</people>

Simple Array Convert

const {json2xml} = require('@pedroentringer/json2xml')

const json = { 
  people: { 
    name: {
      attrs: {
        encode: 'utf8',
        isFullName: true
      },
      value: 'Pedro Entringer'
    }, 
    age: XX,
    mother: {
      name: {
        attrs: {
          encode: 'utf8',
          isFullName: true
        },
        value: 'Pedro Entringer'
      },
      age: XX
    },
    brothers:{
      brother: [
        {
          attrs: {
            id: 1
          },
          value: "Nome"
        },
        {
          attrs: {
            id: 2
          },
          value: "Nome"
        }
      ]
    }
  }
}

const xml = json2xml(json)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<people>
   <name encode="utf8" isFullName="true">Pedro Entringer</name>
   <age>XX</age>
   <mother>
      <name encode="utf8" isFullName="true">Pedro Entringer</name>
      <age>XX</age>
   </mother>
   <brothers>
      <brother id="1">Nome</brother>
      <brother id="2">Nome</brother>
   </brothers>
</people>

Complex Array Convert

const {json2xml} = require('@pedroentringer/json2xml')

const json = { 
  people: { 
    name: {
      attrs: {
        encode: 'utf8',
        isFullName: true
      },
      value: 'Pedro Entringer'
    }, 
    age: XX,
    mother: {
      name: {
        attrs: {
          encode: 'utf8',
          isFullName: true
        },
        value: 'Pedro Entringer'
      },
      age: XX
    },
    brothers:{
      brother: [
        {
          attrs: {
            id: 1
          },
          value: {
              name: "Brother Name",
              age: 'XX',
              hobbies: {
                  hobbie: [
                    {value: 'Hobbie 1'},
                    {value: 'Hobbie 2'},
                  ]
              }
          }
        },
        {
          attrs: {
            id: 2
          },
          value: {
              name: "Brother Name",
              age: 'XX',
              hobbies: {
                  hobbie: [
                    {value: 'Hobbie 1'},
                    {value: 'Hobbie 2'},
                  ]
              }
          }
        }
      ]
    }
  }
}

const xml = json2xml(json)

Result:

<?xml version="1.0" encoding="UTF-8"?>
<people>
   <name encode="utf8" isFullName="true">Pedro Entringer</name>
   <age>XX</age>
   <mother>
      <name encode="utf8" isFullName="true">Pedro Entringer</name>
      <age>XX</age>
   </mother>
   <brothers>
      <brother id="1">
         <name>Brother Name</name>
         <age>XX</age>
         <hobbies>
            <hobbie>Hobbie 1</hobbie>
            <hobbie>Hobbie 2</hobbie>
         </hobbies>
      </brother>
      <brother id="2">
         <name>Brother Name</name>
         <age>XX</age>
         <hobbies>
            <hobbie>Hobbie 1</hobbie>
            <hobbie>Hobbie 2</hobbie>
         </hobbies>
      </brother>
   </brothers>
</people>

About

Convert json to xml

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published