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

How to deserialize XML Prolog? #725

Closed
mmittnacht opened this issue Mar 11, 2024 · 2 comments
Closed

How to deserialize XML Prolog? #725

mmittnacht opened this issue Mar 11, 2024 · 2 comments
Labels
question serde Issues related to mapping from Rust types to XML

Comments

@mmittnacht
Copy link

mmittnacht commented Mar 11, 2024

Let's say I have some xml like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>Test1</test>

How could I create a structure to represent the entire data structure with the prolog?

#[derive(Debug, Deserialize)]
pub struct Data {
    pub xml_prolog: XmlProlog,
    pub test: String
}

#[derive(Debug, Deserialize)]
pub struct XmlProlog {
    pub version: XmlProlog,
    pub encoding: String,
    pub standalone: String
}

Thanks for any help!

@Mingun
Copy link
Collaborator

Mingun commented Mar 11, 2024

XML Prolog (according to the specification, that's all before the root element) is not considered as XML content, so it is not mapped to serde model which works only with document content.

So it is impossible to use serde to read XML declaration as you want. You should create a Reader, read the first event and if it is Event::Decl, process it (maybe you would need to skip Event::Comment and Event::Text events, although in well-formed XML documents nothing can precede the XML declaration, so if you interested only on strictly well-formed documents, expect only Event::Decl).

Unfortunately, it is not possible to get access to the internal reader of existing Deserializer or to create a Deserializer from existing Reader, so some work for parsing will be done twice. We are interested in improvements in that area.

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels Mar 11, 2024
@mmittnacht
Copy link
Author

Hey @Mingun

Thank you for taking time to help me with a well detailed response. In our use-case the double serialization might be sufficient enough, though I will think if I can come up with a better approach later on. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants