Skip to content

joelparkerhenderson/demo-rust-xml-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XML example

XML example that uses Rust and the xml-rs crate to parse a typical XML file.

Example:

demo-rust-xml-rs file.xml

Input file:

<foo>
    <goo>Hello</goo>
</foo>

Output:

StartDocument version:1.0 encoding:UTF-8 standalone:None
  StartElement name:foo atributes:[]
    StartElement name:goo atributes:[]
      Characters Hello
    EndElement goo
  EndElement foo
EndDocument

Steps

The program does these steps:

  1. Open a file path "file.xml".
  2. Cast the file to a BufReader to an EventReader.
  3. Parse the XML elements

XmlEvent

pub enum XmlEvent {
    StartDocument {
        version: XmlVersion,
        encoding: String,
        standalone: Option<bool>,
    },
    EndDocument,
    ProcessingInstruction {
        name: String,
        data: Option<String>,
    },
    StartElement {
        name: OwnedName,
        attributes: Vec<OwnedAttribute>,
        namespace: Namespace,
    },
    EndElement {
        name: OwnedName,
    },
    CData(String),
    Comment(String),
    Characters(String),
    Whitespace(String),
}