From 1413e4a20f8d37eb40756ae1be5fce20c613a58d Mon Sep 17 00:00:00 2001 From: Jia Chao Date: Mon, 3 Jun 2024 11:55:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20XmlReader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jia Chao --- Cargo.toml | 1 + src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 75a79f6..bf92c7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ description = "An xml parser for openEuler cvrf." [dependencies] serde = { version = "1", features = ["serde_derive"] } +xml-rs = "0.8.20" diff --git a/src/lib.rs b/src/lib.rs index e12f9c0..224f6e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,10 +3,46 @@ 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}; -#[derive(Debug, Clone, Serialize, Deserialize)] +struct XmlReader { + // an iterator for XmlEvent + events: EventReader>, + + // 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 { + 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 { // pub documenttitle: String, @@ -326,7 +362,7 @@ impl Product { // ... // ... // -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Vulnerability { // ... pub notes: Vec,