| Crates.io | xml_to_json_rs |
| lib.rs | xml_to_json_rs |
| version | 0.1.1 |
| created_at | 2025-03-26 19:03:19.476664+00 |
| updated_at | 2025-03-26 19:37:53.226805+00 |
| description | Simple rust library to transform XML to json |
| homepage | https://github.com/marcomq/xml_to_json_rs |
| repository | https://github.com/marcomq/xml_to_json_rs |
| max_upload_size | |
| id | 1606946 |
| size | 14,674 |
Rust library to convert an XML string to serde_json::Value
This library is designed to be simple and create serde_json::Value.
It supports
Numbers and Booleans will just be parsed as String, as they also look the same in XML.
The inner text attributes will be wrapped by default into #text and attributes will
get an @, for example @href. By this, the result will be consistent and can
be transformed back to XML. This library is not using $text as this creates
issues with MongoDB.
To transform the JSON back to XML, you can use prepare_for_quick_xml and quick-xml:
let simple_xml = "<a><b>simple</b></a>";
let parser = XmlToJson::default();
let json_value = parser.xml_to_json(xml).unwrap();
let comp_value = parser.prepare_for_quick_xml(json_value);
assert_eq!(xml, quick_xml::se::to_string_with_root("a", &comp_value).unwrap());
Check the tests in src/libs.rs to see further usage examples.
Usage:
let simple_xml = "<a><b>simple</b></a>";
assert_eq!(
XmlToJson::default()
.with_root()
.xml_to_json(simple_xml)
.unwrap(),
json!({ "a": {"b": { "#text": "simple" } } })
);
let nested_with_xml_with_attributes =
"<a><b href=\"#self\">simple</b><b><c class=\"my_class\"><d>D</d><d>1</d></c></b></a>";
assert_eq!(
XmlToJson::default()
.xml_to_json(nested_with_xml_with_attributes)
.unwrap(),
json!({ "b": [{ "@href": "#self", "#text": "simple" } , {"c": { "@class": "my_class", "d": [{ "#text": "D" }, { "#text": "1" }] } } ] })
);