beevik / etree

parse and generate XML easily in go
BSD 2-Clause "Simplified" License
1.47k stars 175 forks source link

expose WriteTo for tokens, define the needed writer interface #96

Closed hugowetterberg closed 3 years ago

hugowetterberg commented 3 years ago

This makes it possible to write XML fragments without having to copy elements to a separate document.

Also satisfies the InnerXML use-case from https://github.com/beevik/etree/issues/71

var innerXML bytes.Buffer
var writeSettings etree.WriteSetting

for _, child := range element.Child {
    child.WriteTo(&innerXML, &writeSettings)
}

In my actual use-case I just passed in &doc.WriteSettings, but there might also be a legitimate need to write out fragments with other settings. So I find a parameter preferrable to using the settings of a hidden associated document.

hugowetterberg commented 3 years ago

Sorry about the double pull requests, needed to free up master for use as a dependency, but couldn't change the pull branch for the PR. New PR: https://github.com/beevik/etree/pull/97