Skip to content

Commit 55cde4f

Browse files
committed
Configuration and basic XML parsing
0 parents  commit 55cde4f

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "xml-to-postgres"
3+
version = "0.1.0"
4+
authors = ["Bart Noordervliet"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
quick-xml = "0.18"
11+
yaml-rust = "0.4"

src/main.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
extern crate quick_xml;
2+
extern crate yaml_rust;
3+
4+
use std::io::Read;
5+
use std::fs::File;
6+
use std::env;
7+
use quick_xml::Reader;
8+
use quick_xml::events::Event;
9+
use yaml_rust::{Yaml, YamlLoader};
10+
11+
#[derive(Debug)]
12+
struct Column {
13+
name: String,
14+
path: String,
15+
value: String
16+
}
17+
18+
fn main() {
19+
let args: Vec<_> = env::args().collect();
20+
if args.len() != 3 {
21+
println!("usage: {} <configfile> <xmlfile>", args[0]);
22+
return;
23+
}
24+
25+
let mut config_str = String::new();
26+
File::open(&args[1]).unwrap().read_to_string(&mut config_str).unwrap();
27+
let config = &YamlLoader::load_from_str(&config_str).unwrap()[0];
28+
29+
let mut reader;
30+
reader = Reader::from_file(&args[2]).unwrap();
31+
reader.trim_text(true);
32+
33+
let mut path = String::new();
34+
let mut buf = Vec::new();
35+
36+
let mut count = 0;
37+
38+
let rowpath = config["rowpath"].as_str().expect("No valid 'rowpath' entry in configuration file");
39+
let colspec = config["columns"].as_vec().expect("No valid 'columns' array in configuration file");
40+
let namefield = &Yaml::from_str("name");
41+
let pathfield = &Yaml::from_str("path");
42+
let mut columns = Vec::new();
43+
44+
for col in colspec {
45+
let hash = col.as_hash().expect("Column entry is not a valid hash");
46+
let name = hash[namefield].as_str().unwrap();
47+
let colpath = hash[pathfield].as_str().unwrap();
48+
println!("Column name {} path {}", name, colpath);
49+
let mut path = String::from(rowpath);
50+
path.push_str(colpath);
51+
columns.push(Column { name: name.to_string(), path: path, value: String::new() });
52+
}
53+
54+
loop {
55+
match reader.read_event(&mut buf) {
56+
Ok(Event::Start(ref e)) => {
57+
path.push('/');
58+
path.push_str(reader.decode(e.name()).unwrap());
59+
if path == rowpath {
60+
count += 1;
61+
}
62+
},
63+
Ok(Event::Text(ref e)) => {
64+
for i in 0..columns.len() {
65+
if path == columns[i].path {
66+
columns[i].value.push_str(&e.unescape_and_decode(&reader).unwrap());
67+
}
68+
}
69+
},
70+
Ok(Event::End(_)) => {
71+
if path == rowpath {
72+
println!("Insert row with id {}", columns[0].value);
73+
for i in 0..columns.len() { columns[i].value.clear(); }
74+
}
75+
let i = path.rfind('/').unwrap();
76+
let _ = path.split_off(i);
77+
},
78+
Ok(Event::Eof) => break,
79+
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
80+
_ => ()
81+
}
82+
buf.clear();
83+
}
84+
println!("{} rows processed", count);
85+
}

0 commit comments

Comments
 (0)