一個 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
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 :-
Parker(optionally with attributes)
Badgerfish
Google Data(GData).
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 :-
Similar named consecutive elements are grouped under the name of the element, not
anonymously in an array as in the rules above, so :-
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 :-
Element names become object properties
Text content of elements goes in the $ property of an object.
Nested elements become nested properties
Multiple elements at the same level become named array elements.
Attributes go in properties whose names begin with @.
Multiple attributes at the same level become array elements.
Active namespaces for an element go in the element's @xmlns property.
The default namespace URI goes in @xmlns.$.
Other namespaces go in other properties of @xmlns.
Elements with namespace prefixes become object properties, too.
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.
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.
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 :-
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.
XML Comments are ignored.
CDATA sections are ignored for Parker but translated into '__cdata' properties
for Badgerfish and GData