@@ -9,19 +9,24 @@ import (
99 "encoding/xml"
1010 "errors"
1111 "html"
12+ "io"
1213)
1314
14- // reparentXML will wrap the given data (which is assumed to be valid XML), in
15- // a fake root nodeAlias.
15+ // reparentXML will wrap the given reader (which is assumed to be valid XML),
16+ // in a fake root nodeAlias.
1617//
1718// This action is useful in the event that the original XML document does not
1819// have a single root nodeAlias, which is required by the XML specification.
1920// Additionally, Go's XML parser will silently drop all nodes after the first
2021// that is encountered, which can lead to data loss from a parser perspective.
2122// This function also enables the ingestion of blank XML files, which would
2223// normally cause a parsing error.
23- func reparentXML (data []byte ) []byte {
24- return append (append ([]byte ("<fake-root>" ), data ... ), "</fake-root>" ... )
24+ func reparentXML (reader io.Reader ) io.Reader {
25+ return io .MultiReader (
26+ bytes .NewReader ([]byte ("<fake-root>" )),
27+ reader ,
28+ bytes .NewReader ([]byte ("</fake-root>" )),
29+ )
2530}
2631
2732// extractContent parses the raw contents from an XML node, and returns it in a
@@ -96,10 +101,9 @@ func extractContent(data []byte) ([]byte, error) {
96101
97102// parse unmarshalls the given XML data into a graph of nodes, and then returns
98103// a slice of all top-level nodes.
99- func parse (data [] byte ) ([]xmlNode , error ) {
104+ func parse (reader io. Reader ) ([]xmlNode , error ) {
100105 var (
101- buf = bytes .NewBuffer (reparentXML (data ))
102- dec = xml .NewDecoder (buf )
106+ dec = xml .NewDecoder (reparentXML (reader ))
103107 root xmlNode
104108 )
105109
0 commit comments