Line data Source code
1 : /*
2 : * Package : xml2json
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 12/09/2013
5 : * Copyright : S.Hamblett@OSCF
6 : *
7 : * The Xml2Json class provids a means of parsing XML data and transforming the
8 : * resultant element tree into the following popular JSON XML formats :-
9 : *
10 : * Parker
11 : * Badgerfish
12 : * GData
13 : *
14 : * The XML parser used is the one supplied by the petitparser package.
15 : *
16 : * Functionality splits XML parsing out from transformation to JSON so as to allow
17 : * clients to extract intermediate results if needed and to transform into more than
18 : * one JSON XML format without re-parsing.
19 : *
20 : * Note this package is intended for use as a one way transform only, it does not
21 : * guarantee the resulting JSON string can be parsed back into valid XML.
22 : *
23 : * See the individual transform classes for further documentation, however, all
24 : * the transform functions convert XML data into JSON strings, so numbers such as
25 : * <mynumber>150</mynumber> become { "mynumber" : "150" } not { "mynumber" : 150 }
26 : *
27 : * If your translating from ATOM then Badgerfish and GData are the better ones
28 : * to use as they are less lossy.
29 : *
30 : * Usage is :-
31 : *
32 : * Xml2Json jsonTransform = new Xml2Json();
33 : * jsonTransform.parse(xmlString);
34 : * String jsonString = jsonTransform.toXxxxxxx();
35 : *
36 : * Any exceptions thrown by the parser/transformers are re-thrown as instances of
37 : * Xml2JsonException.
38 : */
39 :
40 : part of xml2json;
41 :
42 : class Xml2Json {
43 : XmlDocument _result = null;
44 :
45 : /// The parser result
46 1 : XmlDocument get xmlParserResult => _result;
47 :
48 : /// Parse an XML string
49 : void parse(String xmlString) {
50 1 : _result = null;
51 1 : final String xmlStringPrep = _Xml2JsonUtils.prepareXmlString(xmlString);
52 : try {
53 2 : _result = xml.parse(xmlStringPrep);
54 : } catch (e) {
55 2 : final errorString = "parse error - invalid XML, ${e.message}";
56 1 : throw new Xml2JsonException(errorString);
57 : }
58 : }
59 :
60 : /// Badgerfish transformer
61 : String toBadgerfish() {
62 1 : if (_result == null) {
63 1 : throw new Xml2JsonException("toBadgerfish - no parse result");
64 : }
65 :
66 : String json = null;
67 1 : final _Xml2JsonBadgerfish badgerfishTransformer = new _Xml2JsonBadgerfish();
68 : try {
69 2 : json = badgerfishTransformer.transform(_result);
70 : } catch (e) {
71 0 : throw new Xml2JsonException("toBadgerfish error => ${e.toString()}");
72 : }
73 :
74 : return json;
75 : }
76 :
77 : /// Parker transformer
78 : String toParker() {
79 1 : if (_result == null) {
80 1 : throw new Xml2JsonException("toParker - no parse result");
81 : }
82 :
83 : String json = null;
84 1 : final _Xml2JsonParker parkerTransformer = new _Xml2JsonParker();
85 : try {
86 2 : json = parkerTransformer.transform(_result);
87 : } catch (e) {
88 0 : throw new Xml2JsonException("toParker error => ${e.toString()}");
89 : }
90 :
91 : return json;
92 : }
93 :
94 : /// GData transformer
95 : String toGData() {
96 1 : if (_result == null) {
97 1 : throw new Xml2JsonException("toGData - no parse result");
98 : }
99 :
100 : String json = null;
101 1 : final _Xml2JsonGData gDataTransformer = new _Xml2JsonGData();
102 : try {
103 2 : json = gDataTransformer.transform(_result);
104 : } catch (e) {
105 0 : throw new Xml2JsonException("toGData error => ${e.toString()}");
106 : }
107 :
108 : return json;
109 : }
110 : }
|