quick-xml icon indicating copy to clipboard operation
quick-xml copied to clipboard

Please document how to pretty-print

Open tgrushka opened this issue 7 months ago • 2 comments

I see this has been worked on, and the author suggested someone in the community submit a PR to improve documentation. I'm sure someone would happily do that if they knew how!!! The following is what AI helped me figure out, which I don't know if is "the right way" or not -- it seems to work. But it's awfully complicated compared to other libraries that have it built in.

Developers, (why?) do we have to continually, gently remind each other that we cannot read each others' minds? :)

Note how to_xml() is very simple, yet who in the world would figure out how to write to_xml_pretty() without help (because it's not documented)? We are very busy with other major projects. It's not that we can't figure it out. It's that we don't have time (or the patience of saints!) to reverse engineer things like this.

Is it a reasonable request, or not too difficult, to just add docs for this, or a function to do this in se, like to_string_pretty(indent: u8) or similar?


impl Response {
    pub fn to_xml(&self) -> String {
        quick_xml::se::to_string(self).unwrap()
    }

    pub fn to_xml_pretty(&self) -> String {
        let mut buffer = Vec::new();
        let mut writer = Writer::new_with_indent(&mut buffer, b' ', 4);
        /// Why do we have to hard-code the tag name here, when `to_string()` above includes that functionality?
        writer.write_serializable("Response", self).unwrap();
        String::from_utf8(buffer).unwrap()
    }
}

tgrushka avatar May 03 '25 22:05 tgrushka

i think if you started by searching the documentation, you got the result :)

impl Response {
    pub fn to_xml(&self) -> String {
        quick_xml::se::to_string(self).unwrap()
    }

    pub fn to_xml_pretty(&self) -> String {
        let mut buffer = String::new(); 
        let mut serializer = Serializer::new(&mut buffer);
        serializer.indent(b' ', 4);
        self.serialize(serializer).unwrap();
        buffer
    }
}

Yes, I agree, that this example can be added to Serializer::indent and to_string_pretty also could be added.

Writer::write_serializable used as bridge between event-based and serde-based serializers. Serde serializes type, and in XML tag name is not part of the type, but a part of a concrete <element>. I assume, that your Response is enum, that is why to_string works, because in that case root tag name is inferred from the variant name. If it would be struct, it fails.

Mingun avatar May 04 '25 08:05 Mingun

I can relate to that. It took me a bit of time to figure out that indent is what I need. Having a helper to do that is reasonable. However I wonder if indent is the only option that should be considered when printing pretty xml. So perhaps just documenting this somewhere at the top would be good enough to provide a quick snippet how to achieve that without introducing half-good solution.

pronebird avatar May 17 '25 08:05 pronebird