22 个不稳定版本 (5 个破坏性更新)
0.6.12 | 2024 年 7 月 19 日 |
---|---|
0.6.7 | 2024 年 3 月 6 日 |
0.6.5 | 2023 年 12 月 17 日 |
0.6.0 | 2023 年 11 月 27 日 |
#1379 in 解析器实现
每月 365 次下载
120KB
3K SLoC
xml_schema_generator
库和二进制文件,用于将给定的 XML 输入转换为使用 quick_xml 生成的 Struct(作为字符串或文件),您可以在 Rust 程序中使用它来序列化或反序列化相同格式的 XML
您可以使用以下命令添加此依赖项
[dependencies]
xml_schema_generator = "0.6.12"
示例
如何实现库
use quick_xml::reader::Reader;
use xml_schema_generator::{into_struct, Options};
let xml = "<a b=\"c\">d</a>";
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 compatible xml files to extend the structure to match those files as well
// see examples/parse_multiple_xml_rs
您可以在 /examples 目录 中找到更多示例
安装
从源代码安装(如果不需要额外输出,则 env_logger 为可选)
cargo install xml_schema_generator --features="env_logger"
或从 GitHub 下载最新版本
如何运行二进制文件
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 version
Web Assembly
您可以在 xml_schema_generator Github Pages 上查看 xml_schema_generator 的结果
有关贡献详情,请参阅 wasm/README.md
贡献
只需在 github 上创建一个经过良好测试的 Pull Request 即可
实现的功能
☑ 解析 UTF-8 XML 文件
☑ 生成 Rust 结构体
☑ 检测可选属性
☑ 检测可选子元素
☑ 添加二进制文件以独立运行此库
☑ 将 panic! 替换为 Result<>
☑ 添加 serde_xml_rs 支持
☑ 添加 Option 以更改 derive 属性
☑ 将 Options(serde_xml_rs 和 derive 属性)添加到二进制文件
☑ 解析多个 XML 文件到一个结果
☑ 对属性和子元素进行排序以提高可读性
☑ Web Assembly 集成
想法(如有需要或通过 github 请求)
☐ 实现 xml:space(默认|保留)
☐ 正确解析命名空间并在 Rust 结构体中反映它们
☐ 检测数值和布尔字段
☐ 支持 enum
☐ 改进 String、&str 和 [u8] 的实现
☐ 提高性能
☐ 支持 UTF-16
☐ 支持 ISO_2022_JP
许可证
Apache-2.0
依赖项
~2.5–3.5MB
~61K SLoC