Expand description
XML Schema Generator
§Description
Parse an XML file and generate a Rust struct that can be used to deserialize the given XML (or serialize one)
§Examples
How to implement the lib
use quick_xml::reader::Reader;
use xml_schema_generator::{into_struct, Options};
let xml = "<xml>...</xml>";
let mut reader = Reader::from_str(xml);
if let Ok(root) = into_struct(&mut reader) {
let struct_as_string = root.to_serde_struct(&Options::quick_xml_de());
// save this result as a .rs file and use it to (de)serialize an XML document with quick_xml::de::from_str(xml)
}
// you can even parse additional xml files to extend the structure to match those files as well
// see examples/parse_multiple_xml_rsYou find more examples in the /examples directory
§Install
from source (env_logger is optional if you do not require additional output)
cargo install xml_schema_generator --features="env_logger"or download the latest binary at GitHub
How to run the binary
Usage: [RUST_LOG=info] xml_schema_generator [OPTIONS] <INPUT_PATH> [OUTPUT_PATH]
Arguments:
<INPUT_PATH>
xml file that shall be parsed
[OUTPUT_PATH]
rust file to store the result, or none to print to stdout
Options:
-p, --parser <PARSER>
define the parser that is used to parse the resulting struct
[default: quick-xml-de]
[possible values: quick-xml-de, serde-xml-rs]
-d, --derive <DERIVE>
define the #derive attribute to be added to each resulting struct
[default: "Serialize, Deserialize"]
-s, --sort <SORT>
sorting order for attributes and children
[default: unsorted]
Possible values:
- unsorted: the order remains as found in document
- name: sort attributes and children by name (as given in XML). attributes and children are not merged
-h, --help
Print help (see a summary with '-h')
-V, --version
Print versionStructs§
- Element
- Represents an XML element with its structure and characteristics
- Options
- Configuration options for generating Rust structs from XML
Enums§
- Encoding
Error - Errors that can occur during encoding detection or conversion
- Necessity
- Marks an XML element or attribute as optional or mandatory
- Parser
Error - Errors that can occur during XML parsing
- SortBy
- Defines the sorting order for XML attributes and child elements
Functions§
- convert_
to_ utf8 - Convert the input data to a UTF-8 string using encoding detection
- detect_
encoding - Detect the encoding of the given byte data
- extend_
struct - Extends an existing Element tree with data from another XML document
- into_
struct - Parses an XML document into an Element tree structure
- merge_
necessity - Merges two lists of necessities according to intersection rules
- read_
file_ as_ utf8 - Read a file and convert it to UTF-8 string