2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rust】xpathでxmlを検索する方法

Posted at

Rustでxpathを使ってxml検索

amxmlというcrateを使って、xmlを検索します。他にも有用なcrateがあるようです。

$ cargo add amxml
use std::{fs::File, io::Read};

fn main() {
    // contents にファイル全体を読み込み
    let file_name: &str = "src/hoge.xml";
    let mut f: File = File::open(file_name).expect("Fail to open file.");
    let mut contents: String = String::new();
    f.read_to_string(&mut contents).expect("Fail to read file.");

    let document = amxml::dom::new_document(contents.as_str()).unwrap();

    let xpath = document
        .eval_xpath("/Package/types/name[text()='ApexClass']/../members")
        .unwrap();

    for i in 0..xpath.len() {
        let element_name = xpath.get_item(i).as_nodeptr().unwrap().inner_xml();
        println!("{}", element_name);
    }
}

参考文献

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?