xml - rust 和 serde-xml-rs 可以解析 XML 文件中的注释吗?
问题描述
<foo>
<!-- 2021-03-17 08:15:00 EST -->
<row>
<value>100</value>
</row>
</foo>
我想从 XML 文件中获取评论,想知道这是否可能,如果可能,如何?我没有看到这样的例子。在上面的例子中,我想得到“2021-03-17 08:15:00 EST”
解决方案
不,目前 serde-xml-rs 不支持解析 XML 文件中的注释。请参阅源代码中的此处;他们一起跳过评论。
但是有一个开放的拉取请求来添加对解析评论的支持。
因此,如果您现在想解析评论(并声明这是不稳定的,因为您使用某人的 github 分支来执行此操作),您可以通过这种方式使用上述拉取请求作者的分支:
// Cargo.toml contains:
//
// [dependencies]
// serde = {version = "1.0", features = ["derive"]}
// serde-xml-rs = {git = "https://github.com/elmarco/serde-xml-rs.git", branch = "comment"}
use serde::Deserialize;
use serde_xml_rs::from_reader;
#[derive(Debug, Deserialize)]
pub struct Foo {
#[serde(rename = "$comment")]
pub date: Option<String>,
pub row: RowValue,
}
#[derive(Debug, Deserialize)]
pub struct RowValue {
pub value: i32,
}
fn main() {
let input = "<foo> \
<!-- 2021-03-17 08:15:00 EST --> \
<row> \
<value>100</value> \
</row> \
</foo>";
let foo: Foo = from_reader(input.as_bytes()).unwrap();
println!("input={:#?}", foo);
}
推荐阅读
- gatling - 如何在加特林响应中命名检查
- python - 当变量调用函数并且函数包含变量时,如何从变量调用函数?
- javascript - 使用 Moment 和 React JS 格式化日期和时间
- spring - 在 Rest API 中定义端点的最佳约定是什么?
- angular - 如何在 Angular SSR 中获取 BASE_URL?
- javascript - 根据子子集属性从 REST API 获取数据 - 邮递员
- python - 是否有任何特殊输入可以破坏 Python 3 中的 input() 函数?
- ruby - 将函数存储在哈希中键的值中
- ssh - 从 circleci SSH 到 digitalocean 服务器
- python - 规范化熊猫数据框中的列