Skip to content

Commit

Permalink
Merge pull request #975 from tefra/etree-encoder
Browse files Browse the repository at this point in the history
feat: Add xml and lxml tree serializers
  • Loading branch information
tefra authored Mar 11, 2024
2 parents 9da8d63 + d0e2d56 commit 02c6b29
Show file tree
Hide file tree
Showing 15 changed files with 1,642 additions and 1,273 deletions.
57 changes: 57 additions & 0 deletions docs/data_binding/tree_serializing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Element Tree Serializer

The element tree serializers will render an object into an element tree, that you can
use to run XPATH evaluations or XSLT transformations.

There are two implementations based on lxml
[LxmlTreeSerializer][xsdata.formats.dataclass.serializers.LxmlTreeSerializer] and native
python [XmlTreeSerializer][xsdata.formats.dataclass.serializers.XmlTreeSerializer].

## xml.etree.ElementTree.Element

```python
>>> from xml.etree import ElementTree
>>> from tests.fixtures.books.fixtures import books
>>> from xsdata.formats.dataclass.serializers import XmlTreeSerializer
...
>>> serializer = XmlTreeSerializer()
>>> result = serializer.render(books)
...
>>> result.find(".//title").text
'The First Book'

```

## lxml.etree.Element

```python
>>> from lxml import etree
>>> from tests.fixtures.books.fixtures import books
>>> from xsdata.formats.dataclass.serializers import LxmlTreeSerializer
...
>>> serializer = LxmlTreeSerializer()
>>> result = serializer.render(books)
...
>>> etree.indent(result)
>>> actual = etree.tostring(result)
>>> print(actual.decode())
<ns0:books xmlns:ns0="urn:books">
<book>
<author>Hightower, Kim</author>
<title>The First Book</title>
<genre>Fiction</genre>
<price>44.95</price>
<pub_date>2000-10-01</pub_date>
<review>An amazing story of nothing.</review>
</book>
<book>
<author>Nagata, Suanne</author>
<title>Becoming Somebody</title>
<genre>Biography</genre>
<price>33.95</price>
<pub_date>2001-01-10</pub_date>
<review>A masterpiece of the fine art of gossiping.</review>
</book>
</ns0:books>

```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ nav:
- JSON Parsing: data_binding/json_parsing.md
- JSON Serializing: data_binding/json_serializing.md
- Pycode Serializing: data_binding/pycode_serializing.md
- Tree Serializing: data_binding/tree_serializing.md
- Dict Decoding: data_binding/dict_decoding.md
- Dict Encoding: data_binding/dict_encoding.md
- FAQ: faq.md
Expand Down
Loading

0 comments on commit 02c6b29

Please sign in to comment.