Crates.io | rsxml2json |
lib.rs | rsxml2json |
version | 0.1.1 |
source | src |
created_at | 2024-06-02 10:53:44.48648 |
updated_at | 2024-07-09 07:00:22.838012 |
description | A Rust library for converting XML to JSON format |
homepage | |
repository | https://github.com/szy1231/rsxml2json |
max_upload_size | |
id | 1259142 |
size | 32,015 |
A Rust library for converting XML to JSON format
use rsxml2json::{Convert, ConvertConfig};
Example Code 1: Returning String
use rsxml2json::{Convert, ConvertConfig};
fn main() {
//init
let convert = Convert::new(ConvertConfig::default());
//xml data
let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>"#.to_string();
let data_option = convert.execute(xml_str);
let json_str = match data_option {
Ok(value) => value,
Err(_) => return,
};
println!("json_str = {}",json_str);
}
Example Code 2: Returning serde_json::Value
use rsxml2json::{Convert, ConvertConfig};
use serde_json;
fn main() {
// Initialize
let convert = Convert::new(ConvertConfig::default());
// XML data
let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>"#.to_string();
let data_option = convert.execute_json(xml_str);
let json_value = match data_option {
Ok(value) => value,
Err(err) => {
println!("Error: {:?}", err);
return;
}
};
let pretty_json = serde_json::to_string_pretty(&json_value).expect("Unable to convert to pretty JSON");
println!("pretty_json = {}", pretty_json);
}
Input
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<foo>bar</foo>
</osm>
Output
{
"osm": {
"-generator": "CGImap 0.0.2",
"-version": "0.6",
"bounds": {
"-maxlat": "54.0913900",
"-maxlon": "12.2524800",
"-minlat": "54.0889580",
"-minlon": "12.2487570"
},
"foo": "bar"
}
}
Feel free to contribute to this project if you want to fix/extend/improve it.