| 1 |
// Copyright 2012-2014 Charles Banning. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file |
| 4 |
|
| 5 |
// x2j - For (mostly) backwards compatibility with legacy x2j package. |
| 6 |
// Wrappers for end-to-end XML to JSON transformation and value manipulation. |
| 7 |
package x2jscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/ |
| 8 |
|
| 9 |
import ( |
| •10 |
. "github.com/clbanning/mxj/v2scip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/" |
| 11 |
"ioscip-go gomod github.com/golang/go/src go1.15 io/" |
| 12 |
) |
| 13 |
|
| 14 |
// FromXml() --> map[string]interface{} |
| 15 |
func XmlToMapscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlToMap().(xmlVal []byte) (map[string]interface{}, error) { |
| 16 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 17 |
if err != nil { |
| 18 |
return nil, err |
| 19 |
} |
| 20 |
return map[string]interface{}(m), nil |
| 21 |
} |
| 22 |
|
| 23 |
// map[string]interface{} --> ToXml() |
| 24 |
func MapToXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/MapToXml().(m map[string]interface{}) ([]byte, error) { |
| 25 |
return Mapscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#(m).Xmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#Xml().() |
| 26 |
} |
| 27 |
|
| 28 |
// FromXml() --> ToJson(). |
| 29 |
func XmlToJsonscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlToJson().(xmlVal []byte, safeEncoding ...bool) ([]byte, error) { |
| 30 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 31 |
if err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
return m.Jsonscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#Json().(safeEncoding...) |
| 35 |
} |
| 36 |
|
| 37 |
// FromXml() --> ToJsonWriterRaw(). |
| 38 |
func XmlToJsonWriterscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlToJsonWriter().(xmlVal []byte, jsonWriter ioscip-go gomod github.com/golang/go/src go1.15 io/.Writerscip-go gomod github.com/golang/go/src go1.15 io/Writer#, safeEncoding ...bool) ([]byte, error) { |
| 39 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
return m.JsonWriterRawscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#JsonWriterRaw().(jsonWriter, safeEncoding...) |
| 44 |
} |
| 45 |
|
| 46 |
// FromXmlReaderRaw() --> ToJson(). |
| 47 |
func XmlReaderToJsonscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlReaderToJson().(xmlReader ioscip-go gomod github.com/golang/go/src go1.15 io/.Readerscip-go gomod github.com/golang/go/src go1.15 io/Reader#, safeEncoding ...bool) ([]byte, []byte, error) { |
| 48 |
m, xraw, err := NewMapXmlReaderRawscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXmlReaderRaw().(xmlReader) |
| 49 |
if err != nil { |
| 50 |
return xraw, nil, err |
| 51 |
} |
| 52 |
j, jerr := m.Jsonscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#Json().(safeEncoding...) |
| 53 |
return xraw, j, jerr |
| 54 |
} |
| 55 |
|
| 56 |
// FromXmlReader() --> ToJsonWriter(). Handy for bulk transformation of documents. |
| 57 |
func XmlReaderToJsonWriterscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlReaderToJsonWriter().(xmlReader ioscip-go gomod github.com/golang/go/src go1.15 io/.Readerscip-go gomod github.com/golang/go/src go1.15 io/Reader#, jsonWriter ioscip-go gomod github.com/golang/go/src go1.15 io/.Writerscip-go gomod github.com/golang/go/src go1.15 io/Writer#, safeEncoding ...bool) ([]byte, []byte, error) { |
| 58 |
m, xraw, err := NewMapXmlReaderRawscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXmlReaderRaw().(xmlReader) |
| 59 |
if err != nil { |
| 60 |
return xraw, nil, err |
| 61 |
} |
| 62 |
jraw, jerr := m.JsonWriterRawscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#JsonWriterRaw().(jsonWriter, safeEncoding...) |
| 63 |
return xraw, jraw, jerr |
| 64 |
} |
| 65 |
|
| 66 |
// XML wrappers for Map methods implementing tag path and value functions. |
| 67 |
|
| 68 |
// Wrap PathsForKey for XML. |
| 69 |
func XmlPathsForTagscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlPathsForTag().(xmlVal []byte, tag string) ([]string, error) { |
| 70 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
paths := m.PathsForKeyscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#PathsForKey().(tag) |
| 75 |
return paths, nil |
| 76 |
} |
| 77 |
|
| 78 |
// Wrap PathForKeyShortest for XML. |
| 79 |
func XmlPathForTagShortestscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlPathForTagShortest().(xmlVal []byte, tag string) (string, error) { |
| 80 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 81 |
if err != nil { |
| 82 |
return "", err |
| 83 |
} |
| 84 |
path := m.PathForKeyShortestscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#PathForKeyShortest().(tag) |
| 85 |
return path, nil |
| 86 |
} |
| 87 |
|
| 88 |
// Wrap ValuesForKey for XML. |
| 89 |
// 'attrs' are key:value pairs for attributes, where key is attribute label prepended with a hypen, '-'. |
| 90 |
func XmlValuesForTagscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlValuesForTag().(xmlVal []byte, tag string, attrs ...string) ([]interface{}, error) { |
| 91 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 92 |
if err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
return m.ValuesForKeyscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#ValuesForKey().(tag, attrs...) |
| 96 |
} |
| 97 |
|
| 98 |
// Wrap ValuesForPath for XML. |
| 99 |
// 'attrs' are key:value pairs for attributes, where key is attribute label prepended with a hypen, '-'. |
| 100 |
func XmlValuesForPathscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlValuesForPath().(xmlVal []byte, path string, attrs ...string) ([]interface{}, error) { |
| 101 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
return m.ValuesForPathscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#ValuesForPath().(path, attrs...) |
| 106 |
} |
| 107 |
|
| 108 |
// Wrap UpdateValuesForPath for XML |
| 109 |
// 'xmlVal' is XML value |
| 110 |
// 'newTagValue' is the value to replace an existing value at the end of 'path' |
| 111 |
// 'path' is the dot-notation path with the tag whose value is to be replaced at the end |
| 112 |
// (can include wildcard character, '*') |
| 113 |
// 'subkeys' are key:value pairs of tag:values that must match for the tag |
| 114 |
func XmlUpdateValsForPathscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlUpdateValsForPath().(xmlVal []byte, newTagValue interface{}, path string, subkeys ...string) ([]byte, error) { |
| 115 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 116 |
if err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
_, err = m.UpdateValuesForPathscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#UpdateValuesForPath().(newTagValue, path, subkeys...) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
return m.Xmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#Xml().() |
| 124 |
} |
| 125 |
|
| 126 |
// Wrap NewMap for XML and return as XML |
| 127 |
// 'xmlVal' is an XML value |
| 128 |
// 'tagpairs' are "oldTag:newTag" values that conform to 'keypairs' in (Map)NewMap. |
| 129 |
func XmlNewXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlNewXml().(xmlVal []byte, tagpairs ...string) ([]byte, error) { |
| 130 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
n, err := m.NewMapscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#NewMap().(tagpairs...) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
return n.Xmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#Xml().() |
| 139 |
} |
| 140 |
|
| 141 |
// Wrap NewMap for XML and return as JSON |
| 142 |
// 'xmlVal' is an XML value |
| 143 |
// 'tagpairs' are "oldTag:newTag" values that conform to 'keypairs' in (Map)NewMap. |
| 144 |
func XmlNewJsonscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlNewJson().(xmlVal []byte, tagpairs ...string) ([]byte, error) { |
| 145 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 146 |
if err != nil { |
| 147 |
return nil, err |
| 148 |
} |
| 149 |
n, err := m.NewMapscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#NewMap().(tagpairs...) |
| 150 |
if err != nil { |
| 151 |
return nil, err |
| 152 |
} |
| 153 |
return n.Jsonscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#Json().() |
| 154 |
} |
| 155 |
|
| 156 |
// Wrap LeafNodes for XML. |
| 157 |
// 'xmlVal' is an XML value |
| 158 |
func XmlLeafNodesscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlLeafNodes().(xmlVal []byte) ([]LeafNodescip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/LeafNode#, error) { |
| 159 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 160 |
if err != nil { |
| 161 |
return nil, err |
| 162 |
} |
| 163 |
return m.LeafNodesscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#LeafNodes().(), nil |
| 164 |
} |
| 165 |
|
| 166 |
// Wrap LeafValues for XML. |
| 167 |
// 'xmlVal' is an XML value |
| 168 |
func XmlLeafValuesscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlLeafValues().(xmlVal []byte) ([]interface{}, error) { |
| 169 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 170 |
if err != nil { |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
return m.LeafValuesscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#LeafValues().(), nil |
| 174 |
} |
| 175 |
|
| 176 |
// Wrap LeafPath for XML. |
| 177 |
// 'xmlVal' is an XML value |
| 178 |
func XmlLeafPathscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2/x2j`/XmlLeafPath().(xmlVal []byte) ([]string, error) { |
| 179 |
m, err := NewMapXmlscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/NewMapXml().(xmlVal) |
| 180 |
if err != nil { |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
return m.LeafPathsscip-go gomod github.com/clbanning/mxj/v2 . `github.com/clbanning/mxj/v2`/Map#LeafPaths().(), nil |
| 184 |
} |