添加 XmlReader
Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
parent
5926f7f7ee
commit
1413e4a20f
|
@ -12,3 +12,4 @@ description = "An xml parser for openEuler cvrf."
|
|||
|
||||
[dependencies]
|
||||
serde = { version = "1", features = ["serde_derive"] }
|
||||
xml-rs = "0.8.20"
|
||||
|
|
38
src/lib.rs
38
src/lib.rs
|
@ -3,8 +3,44 @@
|
|||
allow(dead_code, unused_imports, unused_variables, unused_mut)
|
||||
)]
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use xml::reader::{EventReader, Events, XmlEvent};
|
||||
|
||||
struct XmlReader {
|
||||
// an iterator for XmlEvent
|
||||
events: EventReader<BufReader<File>>,
|
||||
|
||||
// the depth in xml
|
||||
depth: usize,
|
||||
}
|
||||
|
||||
impl XmlReader {
|
||||
pub fn new(file: File) -> Self {
|
||||
let buffer = BufReader::new(file);
|
||||
let events = EventReader::new(buffer);
|
||||
|
||||
XmlReader { events, depth: 0 }
|
||||
}
|
||||
|
||||
// pull next stream from xml, set the depth as well.
|
||||
pub fn next(&mut self) -> Result<xml::reader::XmlEvent, xml::reader::Error> {
|
||||
let event = self.events.next();
|
||||
match event {
|
||||
Ok(XmlEvent::StartElement { .. }) => {
|
||||
self.depth += 1;
|
||||
}
|
||||
Ok(XmlEvent::EndElement { .. }) => {
|
||||
self.depth -= 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
event
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct CVRF {
|
||||
|
|
Loading…
Reference in New Issue
Block a user