Comments (5)
@skinkie please keep your samples simple for me,
import lxml
from dataclasses import field, dataclass
from xsdata.formats.dataclass.serializers import LxmlTreeSerializer
@dataclass
class Foo:
attr: int = field(metadata={"type":"Attribute"})
value: str = field(metadata={"type":"Text"})
obj = Foo(attr=1, value="foo")
serializer = LxmlTreeSerializer()
element = serializer.render(obj)
print(lxml.etree.tostring(element, pretty_print=True).decode())
<Foo>foo</Foo>
from xsdata.
We completely missed the mark there, the serializer config pretty_print is completely irrelevant on the tree serializer
from xsdata.
I think it isn't irrelevant. If that (replaced) part on the tree is the only part that isn't pretty printed, it is clear that the serializer has something to do with the pretty printing, it is not handled at the lxml.etree.write.
from xsdata.
Please open a new issue, with a simple example for me :)
from xsdata.
Simple examplen work without issues. Replicating it with the given example is the problem.
from typing import List
from lxml import etree
from tests.fixtures.books import BookForm, BooksForm
from tests.fixtures.books.fixtures import books
from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.parsers.config import ParserConfig
from xsdata.formats.dataclass.parsers.handlers import LxmlEventHandler
from xsdata.formats.dataclass.serializers import LxmlTreeSerializer
serializer = LxmlTreeSerializer()
result = serializer.render(books)
open("/tmp/test.xml", "wb").write(etree.tostring(result, pretty_print=True))
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("/tmp/test.xml", parser)
tree.write("/tmp/test2.xml", pretty_print=True)
context = XmlContext()
config = ParserConfig(fail_on_unknown_properties=False)
xml_parser = XmlParser(context=context, config=config, handler=LxmlEventHandler)
books_list: List[BookForm] = []
for book_xml in tree.iterfind(".//book"):
book: BookForm = xml_parser.parse(book_xml, BookForm)
book.id = "XXX"
books_list.append(book)
lxml_serializer = LxmlTreeSerializer(context)
tree3 = etree.parse("/tmp/test.xml", parser) # Because my XmlParser, does not clear the elements
element = tree3.find(".//book")
element.getparent().replace(element, lxml_serializer.render(books_list[0]))
tree3.write("/tmp/test3.xml", pretty_print=True, strip_text=True)
from typing import List
from lxml import etree
from tests.fixtures.artists import metadata, AliasList
from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.parsers.config import ParserConfig
from xsdata.formats.dataclass.parsers.handlers import LxmlEventHandler
from xsdata.formats.dataclass.serializers import LxmlTreeSerializer
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("tests/fixtures/artists/art001.xml", parser)
context = XmlContext()
config = ParserConfig(fail_on_unknown_properties=False)
xml_parser = XmlParser(context=context, config=config, handler=LxmlEventHandler)
aliases: List[metadata.Alias] = []
for alias_element in tree.iterfind(".//{http://musicbrainz.org/ns/mmd-2.0#}alias"):
alias: metadata.Alias = xml_parser.parse(alias_element, metadata.Alias)
alias.value = "XXX"
aliases.append(alias)
alias_list = AliasList(count=len(aliases), alias=aliases)
lxml_serializer = LxmlTreeSerializer(context)
# tree3 = etree.parse("/tmp/test.xml", parser) # Because my XmlParser, does not clear the elements
element = tree.find(".//{http://musicbrainz.org/ns/mmd-2.0#}alias-list")
element.getparent().replace(element, lxml_serializer.render(alias_list))
tree.write("/tmp/alias-list.xml", pretty_print=True, strip_text=True)
from lxml import etree
from tests.fixtures.artists import metadata, AliasList
from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.parsers.config import ParserConfig
from xsdata.formats.dataclass.parsers.handlers import LxmlEventHandler
from xsdata.formats.dataclass.serializers import LxmlTreeSerializer
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("tests/fixtures/artists/art001.xml", parser)
context = XmlContext()
config = ParserConfig(fail_on_unknown_properties=False)
xml_parser = XmlParser(context=context, config=config, handler=LxmlEventHandler)
alias_list = None
for alias_list_xml in tree.iterfind(".//{http://musicbrainz.org/ns/mmd-2.0#}alias-list"):
alias_list: metadata.AliasList = xml_parser.parse(alias_list_xml, metadata.AliasList)
lxml_serializer = LxmlTreeSerializer(context)
element = tree.find(".//{http://musicbrainz.org/ns/mmd-2.0#}alias-list")
element.getparent().replace(element, lxml_serializer.render(alias_list))
tree.write("/tmp/alias-list.xml", pretty_print=True, strip_text=True)
from xsdata.
Related Issues (20)
- Check type hinting, extra parentheses for attribute. HOT 6
- Recursion Limit Reached HOT 3
- extending the SOAP Config NamedTuple... HOT 5
- Do not lose data during serialization after parsing HOT 5
- Parsing XML to a class: A sequence of alternatives is wronly parsed HOT 2
- WSDL generation gets overwritten by imported XSD of the same name HOT 1
- self-referencing field - original case recursion error HOT 1
- Question about preferred way of XML parsing HOT 4
- Lxmltreeserializer str versus list HOT 5
- Rewrite tree serializers to utilize the existing event writers
- SOAP: Stubs are not generated for most operations in EJBCA WSDL HOT 2
- I would like to write huge trees but don't retain the entire tree in memory HOT 4
- Simultaneous parsing sometimes causes "ConverterWarning: Failed to convert value" HOT 4
- xsdata generate file.xsd fails: AttrsFilters.field_definition() missing 1 required positional argument HOT 2
- Cannot generate nested group references with --unnest-classes HOT 1
- Calls to Ruff format/check fail on Windows for large document sets HOT 3
- Question: Ability to add ` field(repr=False)` for specific fields? HOT 2
- Please bring back support for optional lists HOT 2
- schema check on root element HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xsdata.