1.2.5 • Published 6 years ago

json_xml v1.2.5

Weekly downloads
82
License
MIT
Repository
github
Last release
6 years ago

json_xml

  • XML to JSON
  • JSON to XML
  • express middleware( req.body: JSON, req.rawBody: XML )

XML to JSON

const json_xml = require('json_xml')
var jsonObj = json_xml.xml2json(xmlStr)

TYPE 1

<xml>
  <appid>wx2421b1c4370ec43b</appid>
  <attach><![CDATA[attach1]]></attach>
  <attach>attach2</attach>
  <bank_type>1</bank_type>
  <sign></sign>
  <fee_type><is_subscribe><![CDATA[Y]]></is_subscribe></fee_type>
</xml>
{ xml:
   { appid: 'wx2421b1c4370ec43b',
     attach: [ 'attach1', 'attach2' ],
     bank_type: '1',
     sign: '',
     fee_type: { is_subscribe: 'Y' } } }

TYPE 2

<?xml version="1.0" encoding="utf-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="JobSendedDescription">
  <RetCode>Sucess</RetCode>
  <JobID>159142093</JobID>
  <OKPhoneCounts>1</OKPhoneCounts>
  <StockReduced>1</StockReduced>
  <ErrPhones />
</ROOT>
{ ROOT:
   { RetCode: 'Sucess',
     JobID: '159142093',
     OKPhoneCounts: '1',
     StockReduced: '1',
     ErrPhones: '' } }

JSON to XML

const json_xml = require('json_xml')
var jsonObj = json_xml.xml2json(jsonObj)
{ xml:
   { appid: 'wx2421b1c4370ec43b',
     attach: [ 'attach1', 'attach2' ],
     bank_type: '1',
     sign: '',
     fee_type: { is_subscribe: 'Y' } } }
<xml>
  <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
  <attach><![CDATA[attach1]]></attach>
  <attach><![CDATA[attach2]]></attach>
  <bank_type><![CDATA[1]]></bank_type>
  <sign><![CDATA[]]></sign>
  <fee_type>
    <is_subscribe><![CDATA[Y]]></is_subscribe>
  </fee_type>
</xml>

As Middleware

const express = require('express')
const app = express()
const app = express()

app.use(json_xml.middleware)
app.post('/', (req, res) => {
    console.log(req.rawBody) // XML
    console.log(req.body) // JSON
    res.send('success')
})

Examples

1.2.5

6 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago