Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add xml and lxml tree serializers #975

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading