Viewing version 6.2.6. Open the latest package page.

xml2json

6.2.6

An XML to JSON conversion package. Supports Badgerfish, GData, OpenRally and Parker conversions.

Download archive

Readme

xml2json

Build Status

An XML to JSON conversion package.

This package allows the parsing of XML strings and the transformation of the resulting parse tree into the following XML JSON conventions :-

  1. Parker(optionally with attributes)
  2. Badgerfish
  3. Google Data(GData).
  4. OpenRally

The package uses the xml parser to perform the parsing of XML data into a parse tree.

Exact transforming rules can be found below.

The Parker convention is a lossy but lean conversion from XML into JSON, ideal for lightweight XML parsing of collections of books, records etc. where attributes and namespace information is not needed.

Badgerfish and GData both preserve XML information such as attributes and namespaces. These are more suited to heavyweight XML parsing of say ATOM feeds where this information may be needed.

See the main Xml2Json class for usage.

Xml2Json - details of the JSON output of the transformers

The rules each transformer uses are documented below, following that a global rules section describes rules all the transformers obey.

Parker

The Parker transformer follows the rules documented here with the following caveats :-

  1. Similar named consecutive elements are grouped under the name of the element, not anonymously in an array as in the rules above, so :-

    <root><item>1</item><item>2</item><item>three</item></root>

    becomes

    {"item":["1","2","three"]}

    NOT

    ["1","2","three"]

    as per the rules.

    This allows grouped elements to be extracted by name in one go, i.e. from above all the items can be referenced through the 'item' property. This is better for the usage this transform is suited to, the parsing of simple list like structures of books, records etc that just happen to be in XML. There is also an alternative Parker transformer that transforms node elements that have attributes. The standard Parker transformer should suffice for most use cases.

Badgerfish

The Badgerfish transformer follows the rules documented here, to summarise :-

  1. Element names become object properties
  2. Text content of elements goes in the $ property of an object.
  3. Nested elements become nested properties
  4. Multiple elements at the same level become named array elements.
  5. Attributes go in properties whose names begin with @.
  6. Multiple attributes at the same level become array elements.
  7. Active namespaces for an element go in the element's @xmlns property.
  8. The default namespace URI goes in @xmlns.$.
  9. Other namespaces go in other properties of @xmlns.
  10. Elements with namespace prefixes become object properties, too.
  11. The @xmlns property goes only in the object relative to the element where namespace was declared.

Badgerfish also has the option of using local xml node names by setting the useLocalNameForNodes parameter to true when transforming. This has the effect that since the element key("soapenv" in the example below) is implementation dependent (i.e. one manufacturer will assign "soapenv as the key, while another might assign "ENV"), using the key in the node name makes the resulting json from xml2json dependent on the implementation as well.

For example, this xml :-

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Header/> <soapenv:Body>
<tns:getDataResponse xmlns:tns="https://urchin.com/api/urchin/v1/"> <record> <recordId>1</recordId>
<dimensions> <dimension name="u:month">2008-02-00T00:00:00Z</dimension> </dimensions> <metrics>
<u:hits xmlns:u="https://urchin.com/api/urchin/v1/">836</u:hits>
<u:bytes xmlns:u="https://urchin.com/api/urchin/v1/">1953960</u:bytes> </metrics> </record>
</tns:getDataResponse> </soapenv:Body> </soapenv:Envelope>

gives this JSON output if useLocalNameForNodes is set true +-

{
  "Envelope": {
    "@xmlns": [{ "soapenv": "http://www.w3.org/2003/05/soap-envelope" }],
    "Header": {},
    "Body": {
      "getDataResponse": {
        "@xmlns": [{ "tns": "https://urchin.com/api/urchin/v1/" }],
        "record": {
          "recordId": { "$": "1" },
          "dimensions": {
            "dimension": { "@name": "u:month", "$": "2008-02-00T00:00:00Z" }
          },
          "metrics": {
            "hits": {
              "@xmlns": [{ "u": "https://urchin.com/api/urchin/v1/" }],
              "$": "836"
            },
            "bytes": {
              "@xmlns": [{ "u": "https://urchin.com/api/urchin/v1/" }],
              "$": "1953960"
            }
          }
        }
      }
    }
  }
}

if set to false(default) this JSON response is given :-

{
  "soapenv:Envelope": {
    "@xmlns": [{ "soapenv": "http://www.w3.org/2003/05/soap-envelope" }],
    "soapenv:Header": {},
    "soapenv:Body": {
      "tns:getDataResponse": {
        "@xmlns": [{ "tns": "https://urchin.com/api/urchin/v1/" }],
        "record": {
          "recordId": { "$": "1" },
          "dimensions": {
            "dimension": { "@name": "u:month", "$": "2008-02-00T00:00:00Z" }
          },
          "metrics": {
            "u:hits": {
              "@xmlns": [{ "u": "https://urchin.com/api/urchin/v1/" }],
              "$": "836"
            },
            "u:bytes": {
              "@xmlns": [{ "u": "https://urchin.com/api/urchin/v1/" }],
              "$": "1953960"
            }
          }
        }
      }
    }
  }
}

GData

This is the Google Data(GData) format specified here

This is basically the same as Badgerfish except that it drops the @ symbol for attributes and uses $t instead of just $ for values, also the XML Processing element is included at the top of the transform.

OpenRally

This is the Open Rally format specified here

This is basically the same as Badgerfish except that it drops the @ symbol for attributes and uses (key/value) format for values, also the XML Processing element is included at the top of the transform.

Global Rules

Each transformer implements the following rules :-

  1. All JSON output is in the form of strings, numeric literals, true and false become strings. This allows the output to be 'transmission ready' if needed for RESTful interfaces.

  2. XML Comments are ignored.

  3. CDATA sections are ignored for Parker but translated into '__cdata' properties for Badgerfish and GData

Changelog

6.2.6

Issue 71

6.2.5

Issue 67

6.2.4

Update to issue 23

6.2.3

Added topics and screenshot

6.2.2

Issue 64

6.2.1

Issue 62

6.2.0

Issue 23 - OpenRally update

6.1.0

Issue 58

6.0.0

Issue 60 - Dart 3

5.3.6

Issue 52

5.3.5

Issue 51

5.3.4

Issue 48

5.3.3

PR 46 and issue 47, remove Travis

5.3.2

PR's 44 and 45, add SBOM

5.3.1

Issue 41

5.3.0

Issue 36

5.2.0

Issue 33, addition of Parker with attributes transformer.

5.1.0

Merge pull request #32 from faithoflifedev/master Badgerfish transform now supports the option to use local name for nodes.

5.0.0

Issues 27(NNBD), 28, 29 and 30

5.0.0-nullsafety.0

Null safety first update

4.4.0

Issue 23

4.3.0

Issue 21

4.2.1

Issue 20

4.2.0

Issue 16

4.1.1

Linter updates

4.1.0

Pull request 18, add CDATA node parsing for Parker

4.0.0

Issue 17, License is now MIT, small tidy ups

3.0.3

Update testing, new linter rules

3.0.2

Update testing, run browser tests on Travis

3.0.1

Issue 15, relax sdk constraints for flutter

3.0.0

Issue 14, update to Dart 2

2.0.14

Issue 12, update to latest XML package

2.0.13

Issue 11, exception message bug

2.0.12

Remove dev infinity constraint on the Dart SDK for Flutter

2.0.11

Pub updates

2.0.10

Rename the analysis options file

2.0.9

Issue 9, SDK version constraint update, general pre Dart 2.0 spruce up

2.0.8

Issue 8, correctly escape CDATA strings for JSON

2.0.7

Issue 7, CDATA nodes now translated for Badgerfish and GData.

2.0.6

Use the new XML Package, update to latest Petit Parser see issue 5 in github.

Remove Drone IO integration, now done by pub.

2.0.5

Integrate with Dart's Drone IO testing

2.0.4

Browser is now a dev dependancy

2.0.3

Fixes for Dart V1.5 usage

2.0.2

Remove references to runtime type

2.0.1

Update dependencies to latest

2.0.0

Updates for Dart 1.0 release

1.1.0

  • Fix the runtime type name change intoroduced in Dart r28101
  • Generate API documents
  • Package formatting updates

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'xml2json:6.2.6'
pubspec.yaml
name: xml2json
version: 6.2.6
description: An XML to JSON conversion package. Supports Badgerfish, GData, OpenRally and Parker conversions.
repository: https://github.com/shamblett/xml2json
homepage: https://github.com/shamblett/xml2json

funding:
  - https://www.darticulate.com/#funding

environment:
  sdk: '>=3.0.0 <4.0.0'

dependencies:
  xml: '^6.5.0'

dev_dependencies:
  test: '^1.25.14'
  lints: '^5.1.1'
  build_runner: '^2.4.14'
  build_test: '^2.2.3'
  build_web_compilers: '^4.1.0'

topics:
  - conversion
  - network
  - web
  - xml
  - json

screenshots:
  - description: 'Simple XML to JSON convertor'
    path: example/screenshots/xml2json.png

Version history

VersionPublishedArchiveSHA-256
1.0.0Cached · Upstream SHA-256 matchedDownload729f7983…2feb23
1.1.0Cached · Upstream SHA-256 matchedDownload39b03e07…85bf84
2.0.0Cached · Upstream SHA-256 matchedDownloadaf7d3ee6…ec0c2d
2.0.1Cached · Upstream SHA-256 matchedDownload169aa9ba…a4da67
2.0.2Cached · Upstream SHA-256 matchedDownload40e45618…42f346
2.0.4Cached · Upstream SHA-256 matchedDownloadea270bd7…defd6b
2.0.5Cached · Upstream SHA-256 matchedDownloade4b8ebab…a92176
2.0.6Cached · Upstream SHA-256 matchedDownload49012bfa…453915
2.0.7Cached · Upstream SHA-256 matchedDownload93b8b24b…8b40d9
2.0.8Cached · Upstream SHA-256 matchedDownload6a90025d…e7fea8
2.0.9Cached · Upstream SHA-256 matchedDownload2b2e6b8e…0e2fdc
2.0.10Cached · Upstream SHA-256 matchedDownload9ccc60e3…4b40f7
2.0.11Cached · Upstream SHA-256 matchedDownload2f836ee9…2c0ba5
2.0.12Cached · Upstream SHA-256 matchedDownloadb5e64856…aef215
2.0.13Cached · Upstream SHA-256 matchedDownloadf10d2f1d…21e1b3
2.0.14Cached · Upstream SHA-256 matchedDownload05153eb1…b41c1d
3.0.0Cached · Upstream SHA-256 matchedDownload9210a2bd…62538b
3.0.1Cached · Upstream SHA-256 matchedDownloadfb9b60bf…0fdc51
3.0.2Cached · Upstream SHA-256 matchedDownload372702a8…b63404
3.0.3Cached · Upstream SHA-256 matchedDownloadac907944…bc8e10
4.0.0Cached · Upstream SHA-256 matchedDownloadc9524d8c…12b3b4
4.1.0Cached · Upstream SHA-256 matchedDownload3e78ae12…7ac7d0
4.1.1Cached · Upstream SHA-256 matchedDownload938c4de5…49bdb5
4.2.0Cached · Upstream SHA-256 matchedDownloadff568ea4…47a828
4.2.1Cached · Upstream SHA-256 matchedDownload78a1f838…02e597
4.3.0Cached · Upstream SHA-256 matchedDownloade1f46670…923916
4.4.0Cached · Upstream SHA-256 matchedDownload57ff11ee…d4b46c
5.0.0-nullsafety.0Cached · Upstream SHA-256 matchedDownload1f89b0de…e92d78
5.0.0Cached · Upstream SHA-256 matchedDownloadb67f1bdb…f1d652
5.1.0Cached · Upstream SHA-256 matchedDownloadb72ed366…c3ee2d
5.2.0Cached · Upstream SHA-256 matchedDownload3ee2d132…5a6ba2
5.3.0Cached · Upstream SHA-256 matchedDownload39aa563d…eec41d
5.3.1Cached · Upstream SHA-256 matchedDownload3f0ecacc…03ec22
5.3.2Cached · Upstream SHA-256 matchedDownloadb32ce8fe…60cbf9
5.3.3Cached · Upstream SHA-256 matchedDownload6430701d…c56ae7
5.3.4Cached · Upstream SHA-256 matchedDownload176202b1…3c1d5d
5.3.5Cached · Upstream SHA-256 matchedDownloadbaaed668…65a70e
5.3.6Cached · Upstream SHA-256 matchedDownloadc8cb35b8…f32095
6.0.0Cached · Upstream SHA-256 matchedDownloadc39bf228…d83e52
6.1.0Cached · Upstream SHA-256 matchedDownload12ebc382…5ba975
6.2.0Cached · Upstream SHA-256 matchedDownload1cd5d234…24ea7d
6.2.1Cached · Upstream SHA-256 matchedDownloadffb6dc01…a57675
6.2.2Cached · Upstream SHA-256 matchedDownloadd97aed12…c8eefb
6.2.3Cached · Upstream SHA-256 matchedDownload52b7c8d3…e27bc0
6.2.4Cached · Upstream SHA-256 matchedDownloaddbe79a85…d5b5a8
6.2.5Cached · Upstream SHA-256 matchedDownload9523203b…391664
6.2.6Cached · Upstream SHA-256 matchedDownload9ff65c14…0da142
6.2.7Cached · Upstream SHA-256 matchedDownload8a7ae63b…8a9702
6.2.8Cached · Upstream SHA-256 matchedDownload3ce4ee6b…a2e1f2
6.2.9Cached · Upstream SHA-256 matchedDownload0c1cb4e8…b7bb9b

Dependencies

xml "^6.5.0"