Who is Xavier?
Introducing Xavier: A Simplified XML Parsing Library Inspired by Serde. Why Xavier? Well... it starts with X, and it's the first name that came out of my mind, nothing else.
Xavier is a lightweight and versatile XML parsing library designed to streamline the process of handling XML data with ease and efficiency.
While speed is a consideration in Xavier's design, it's important to emphasize that raw speed isn't its primary goal. Instead, Xavier prioritizes ease of use and ergonomic design, aiming to simplify XML parsing tasks within Rust applications without sacrificing reliability or developer experience.
It must be used in relatively small xml because it stores all data in memory.
Note 1: UTF-16 is not supported yet. Hard work! PR's are welcome. .
Why not extend Serde?
Someone already did that, but I prefer to start from scratch. Besides, since Xavier focuses specifically on XML parsing, I believe it should be simpler and more tailored to that purpose.
What's new?
0.1.7
-
Significantly expanded test coverage.
-
Added support for #[xml(skip)] on primitive fields (field must be Option).
-
Added support for #[xml(inner = "tag")] to deserialize/serialize Vec<...> in the form:
...
...
...
- Enhanced handling of smart pointers (Box, Rc, Arc, ...) within field chains, providing greater flexibility.
- Changes in element naming for a more conventional approach.
This still new lib so please report all bugs and help us!
Examples
Serialize
Starting simple:
This is the simplest example possible:
// ...
println!;
// ...
Should produce:
Some Content A
0
0.0
Names
Improving the names:
// ...
println!;
// ...
Should produce:
Some Content A
0
0.0
Note 1: Using camel config will produce to all elements use the same convention.
Note 2: All cases supported by convert_case crate can be used, except Randoms.
Note 3: ignore_case can be used to ignore case in an element.
Namespace
Working with namespaces:
// ...
let xmlns = namespaces!;
XMLObject
//...
println!;
// ...
Should produce:
Some Content A
0
0.0
Note:
#[xml(xmlns)]
must be used only on root and only one time.
Attributes
Working with attributes:
// ...
println!;
// ...
Should produce:
0
0
Note: use_suffix="false" or use_prefix="true" can be used to force suffix or prefix.
Enum
Working with enums:
// Many libs don't implement of infer any string value in this case, we are no exception.
// ...
println!;
// ...
Should produce:
ValueA
Unnamed Struct
Using a unit struct like this:
;
Should produce:
Some Text
Note: More than one attribute in this case is not supported and will produce compile error.
Unit Struct
Using a unit struct like this:
;
Should produce:
Not so useful as root element... but think about using it as flag field in a more tree context.
Trees
Composing structs like this:
Should produce:
Some value
Other value
Note: Case has the scope of the element. Same for namespaces.
Note: Attention to the fact that the name of child in this case is dictated by the field name. More about this in
test_suite/src/tests/core/names.rs
.
Note:
#[xml(tree)]
is obligated in cases where you have a reference to nested struct.
Collections
Composing structs like this:
Should produce:
Some Text
Child A
Child B
Note:
HashMap<String, T: XmlSerializable>
is also supported but with no naming effect.
Note: Please note that
#[inner="my_child"]
has the name of subitems.
Structs as tags
Configuring nested struct as this:
Should produce:
Some value
Other value
Note 1: You can have as many attribute as you want, but just one value! Note 2: If not specified the default behaviour for a field is attribute, with empty value.
XML declaration
You can configure XML like this:
// or
Should produce:
...
Note: If not specified the default declaration is used with
version="1.0" encoding="UTF-8" standaline = "no"
DTD
Using this:
Should produce:
...
Note 1: Inline DTD is not supported at the moment. However, I'm open to exploring alternative methods. Pull requests are welcome and appreciated. Note 2: XML validation is out of scope of this project.
PI (processing instruction)
Using this:
Should produce:
...
Convenience
CDATA
This:
println!;
Prints this:
<![CDATA[Some text & others]]>
Text encoded
println!;
Prints this:
Some text & others
Comment
This:
println!;
Prints this:
<!--Some text & others-->
Deserialize
Starting simple:
This is the simplest example possible:
// ...
let xml = r#"
<XMLObject>
<some_string>Some Content A</some_string>
<some_int>0</some_int>
<some_float>0.0</some_float>
</XMLObject>"#
let instance: XMLObject = from_xml?;
assert_eq!;
assert_eq!;
assert_eq!;
// ...
As you can see this is the same structure of tags as in serialize. Check out a lot of examples HERE!
Names, Attributes, Enum, Unnamed Struct, Unit Struct, Trees, Collections and Structs as tags
Works exactly like serialize but in opposite direction. Same tags! 😊
Convenience
XML declaration
Declarations can be parsed using this macro!
let = declaration!;
DTD
DTD's can be parsed using this macro!
let = dtd!;
PI (processing instruction)
PI's can be parsed using this macro!
instructions!;
Text decode
println!;
Prints this:
Some text & others
Namespaces
Will be available as a normal tag attribute.
Errors
Xavier DOM (WIP) implementation use DOMException
due to spec, but "Xavier DeSer tiene un PError" ʕ•ᴥ•ʔ
Final Tip:
Most common cases are already covered in
test_suite/src/test/core/*
, which you can use as a more complete reference. If you have any doubts, feel free to ask here or on GitHub — you’re more than welcome!