etree icon indicating copy to clipboard operation
etree copied to clipboard

Exposing more API functions

Open suntong opened this issue 6 years ago • 3 comments

I'd think it'd be helpful to expose the following API functions

doc.NewDocumentFromString(s string), based on:

https://github.com/beevik/etree/blob/4ec130554028d83ef14920b2661effbf0ee275e3/etree_test.go#L14-L22

and,

Element.WriteTo(w *bufio.Writer, s *WriteSettings), for debugging purposes, based on:

https://github.com/beevik/etree/blob/4ec130554028d83ef14920b2661effbf0ee275e3/etree.go#L1036-L1060

Please consider. thx.

suntong avatar Feb 26 '19 17:02 suntong

In the interest of keeping the etree package minimal, I would prefer not to add NewDocumentFromString. It doesn't save the programmer much code.

Exposing a public WriteTo method from Element is an interesting idea. A couple things I'm unsure about:

  1. To use it, you would need to pass a WriteSettings parameter. This makes the method less easy to use. For Document types, which already support WriteTo, the WriteSettings are embedded in the document, making WriteTo much easier to use in this case.
  2. I need to evaluate whether other token types (e.g., CharData, Comment) would also need to support WriteTo for consistency's sake.

beevik avatar Aug 27 '19 23:08 beevik

Good call, thanks for considering. it'd be helpful, but may not be necessary...

suntong avatar Aug 29 '19 02:08 suntong

@beevik, adding WriteTo method would be useful for me too for more efficient XML generation. For now I have to use Element.Copy() and create a new document with this single element so I can invoke WriteTo on it.

Also there is a thing to consider: if there are some unbound namespaces within the element, should Element.WriteTo() method find them and bound using information from parent elements in original Document...

egorbunov avatar Jan 20 '20 20:01 egorbunov

Closing this issue, as it is quite old. There is also a pull request for it.

beevik avatar May 07 '23 20:05 beevik