MCPcopy Create free account
hub / github.com/aws/smithy-go / StructToXML

Function StructToXML

testing/xml/xmlToStruct.go:135–193  ·  view source on GitHub ↗

StructToXML writes an XMLNode to a xml.Encoder as tokens.

(e *xml.Encoder, node *XMLNode, sorted bool)

Source from the content-addressed store, hash-verified

133
134// StructToXML writes an XMLNode to a xml.Encoder as tokens.
135func StructToXML(e *xml.Encoder, node *XMLNode, sorted bool) error {
136 var err error
137 // Sort Attributes
138 attrs := node.Attr
139 if sorted {
140 sortedAttrs := make([]xml.Attr, len(attrs))
141 for _, k := range node.Attr {
142 sortedAttrs = append(sortedAttrs, k)
143 }
144 sort.Sort(xmlAttrSlice(sortedAttrs))
145 attrs = sortedAttrs
146 }
147
148 st := xml.StartElement{Name: node.Name, Attr: attrs}
149 e.EncodeToken(st)
150 // return fmt.Errorf("encoder string : %s, %s, %s", node.Name.Local, node.Name.Space, st.Attr)
151
152 if node.Text != "" {
153 e.EncodeToken(xml.CharData([]byte(node.Text)))
154 } else if sorted {
155 sortedNames := []string{}
156 for k := range node.Children {
157 sortedNames = append(sortedNames, k)
158 }
159 sort.Strings(sortedNames)
160
161 for _, k := range sortedNames {
162 // we should sort the []*xml.Node for each key if len >1
163 flattenedNodes := node.Children[k]
164 // Meaning this has multiple nodes
165 if len(flattenedNodes) > 1 {
166 // sort flattened nodes
167 flattenedNodes, err = sortFlattenedNodes(flattenedNodes)
168 if err != nil {
169 return err
170 }
171 }
172
173 for _, v := range flattenedNodes {
174 err = StructToXML(e, v, sorted)
175 if err != nil {
176 return err
177 }
178 }
179 }
180 } else {
181 for _, c := range node.Children {
182 for _, v := range c {
183 err = StructToXML(e, v, sorted)
184 if err != nil {
185 return err
186 }
187 }
188 }
189 }
190
191 e.EncodeToken(xml.EndElement{Name: node.Name})
192 return e.Flush()

Callers 1

SortXMLFunction · 0.85

Calls 2

xmlAttrSliceTypeAlias · 0.85
sortFlattenedNodesFunction · 0.85

Tested by

no test coverage detected