Insert an XML string as a (raw) element #1143
Replies: 3 comments 1 reply
-
|
I think my question has some part in common with #842, but the other way around. |
Beta Was this translation helpful? Give feedback.
-
|
As a workaround for my problem I have now the following code, that parses the XML payload, that I get and injects it into the object tree so that it can be returned. But this is not very nice. from dataclasses import dataclass, field
from typing import Optional
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.serializers import XmlSerializer
@dataclass
class WrapperType:
"""The Type to hold any RDF data"""
class Meta:
name = "wrapper"
other_element: Optional[object] = field(
metadata={
"type": "Wildcard",
"namespace": "##any",
},
)
@dataclass
class RecordType:
class Meta:
name = "Record"
header: str = field(default="Some info", metadata={"type": "Attribute"})
wrapper: Optional[WrapperType] = field(
default=None,
metadata={
"type": "Element",
},
)
rdf_string = """<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="urn:id:record2"><dc:title>Record 2</dc:title></rdf:Description>
<rdf:Description rdf:about="urn:id:record3"><dc:title>Record 3</dc:title></rdf:Description>
</rdf:RDF>
"""
rdf_data = rdf_string.splitlines()
metadata_string = f"""{rdf_data[0]}
<wrapper>
{"\n".join(rdf_data[1:])}
</wrapper>
"""
model = RecordType(
header="Here comes the data",
wrapper=XmlParser().from_string(metadata_string, WrapperType),
)
output = XmlSerializer().render(model)
print(output)The output is: but the namespaces are replaced by ns0 … which is not nice, but works. I have also experimented with a custom |
Beta Was this translation helpful? Give feedback.
-
|
To demonstrate the use case from a different perspective I have opened: #1161 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have generated the data classes. At one point the schema allows arbitrary XML, I'm generating the XML string to be contained in this element with a different library. How can I inject this raw XML string into the dataclass element tree? If I just pass it as plain string into the constructor, it is serialized to the document but as escaped string, i.e.:
Beta Was this translation helpful? Give feedback.
All reactions