搜索插件、代码仓与 APP 分类

插件详情v6.2.9

xml2json

一个将 XML 转换为 JSON 的包。支持 Badgerfish、GData、OpenRally 和 Parker 转换。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
12万
喜欢数
126
Pub 分数
0

安装插件

flutter pub add xml2json

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,可将 XML 数据转换为 JSON 格式,支持多种转换标准,包括 Badgerfish、GData、OpenRally 和 Parker。它能够无缝集成使用 XML 的 API 和服务,通过将其转换为更易访问的 JSON 结构。该包轻量且专为跨平台使用而设计,非常适合针对 Android、iOS、Web、macOS、Windows 和 Linux 的应用。它简化了接收 XML 响应的应用程序中的数据解析。

适合场景

  • 解析 XML API 响应
  • 将旧版 XML 数据转换为 JSON
  • 与基于 XML 的 Web 服务集成
  • 简化跨平台应用中的数据处理
  • 从 XML 到 JSON 工作流的迁移

主要特点

  • 支持多种 XML 到 JSON 的转换样式
  • 轻量且易于集成
  • 跨平台兼容性
  • 处理嵌套的 XML 结构
  • 提供可配置的转换选项

适合对象

  • 使用 XML API 的开发人员
  • 需要数据转换的应用程序
  • 从 XML 迁移到 JSON 的项目
  • 全栈 Dart 开发人员
  • 使用结构化数据的移动和Web应用

使用建议

  • 转换行为因样式而异(Badgerfish、GData 等)
  • 可能需要验证输入的XML
  • 未经预处理,不适合复杂的XML架构
  • 输出结构取决于所选的转换方法
  • 仅限XML转JSON,无反向功能

原始 README

以下为关联源码仓库的 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

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目