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 #97

Closed hugowetterberg closed 1 year 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.

beevik commented 1 year ago

I really like your proposed change. Could you please resolve conflicts and update the pull request?

I realize it's been nearly 3 years since you submitted this pull request, so I apologize sincerely for not seeing it sooner. I will understand if you are not interested in updating the pull request.

hugowetterberg commented 1 year ago

No worries, and thank you for your work in creating and maintaining etree. It has been of great help when working with XML in go.

Unfortunately I have changed jobs since I made the PR, so I don't have write access to the Infomaker repository any more. But I'll make a private fork and sort out the conflicts in a separate PR.

beevik commented 1 year ago

Thanks for the update. I've committed this change with a few modifications. I renamed XMLWriter to Writer since I felt the "XML" was extraneous. I also added a unit test and the ability to indent a fragment starting with an Element token.