https://github.com/mabackma/json_to_xml
Rust tool for converting JSON to XML.
https://github.com/mabackma/json_to_xml
formatting-data json xml
Last synced: about 1 month ago
JSON representation
Rust tool for converting JSON to XML.
- Host: GitHub
- URL: https://github.com/mabackma/json_to_xml
- Owner: mabackma
- License: mit
- Created: 2025-11-13T01:35:05.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2025-11-19T09:39:59.000Z (6 months ago)
- Last Synced: 2025-12-13T15:00:00.804Z (5 months ago)
- Topics: formatting-data, json, xml
- Language: Rust
- Homepage:
- Size: 99.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON to XML
[Crate](https://crates.io/crates/json_to_xml)
[Documentation](https://docs.rs/json_to_xml/0.1.8/json_to_xml/)
## Convert JSON to XML with a default "Root" element.
This is a convenience function that calls `json_to_xml_with_root` with "Root" as the default root element name.
## Example 1
```rust
use json_to_xml::generate_xml::json_to_xml;
let json_string = r#"
{
"@library": "lib1",
"book": {
"@isbn": "978-3-16-148410-0",
"title": "The Rust Programming Language",
"author": "Steve Klabnik and Carol Nichols"
}
}
"#;
let xml_string = json_to_xml(&json_string).unwrap();
println!("{}", xml_string);
```
## Expected Output (XML):
```xml
Steve Klabnik and Carol Nichols
The Rust Programming Language
```
## Example 2
```rust
use json_to_xml::generate_xml::json_to_xml;
let json_string = r#"
{
"book": {
"@isbn": "978-3-16-148410-0",
"title": "The Rust Programming Language",
"author": "Steve Klabnik and Carol Nichols"
}
}
"#;
let xml_string = json_to_xml(&json_string).unwrap();
println!("{}", xml_string);
```
## Expected Output (XML):
```xml
Steve Klabnik and Carol Nichols
The Rust Programming Language
```
## Convert JSON to XML with a custom root element.
This function takes a JSON string and a specified root element name and converts it into an XML string.
It processes JSON objects, arrays, and primitive values recursively.
Attributes in JSON (prefixed with `@`) are converted to XML attributes.
All XML tags are capitalized.
## Example
```rust
use json_to_xml::generate_xml::json_to_xml_with_root;
let json_string = r#"
{
"@xmlns:addr": "http://standards.fi/schemas/personData/addresses",
"@xmlns:pr": "http://standards.fi/schemas/personData/person",
"person": {
"name": "John Doe",
"age": "30",
"@id": "1234",
"addresses": [
{
"street": "123 Main St",
"city": "Springfield",
"@type": "primary"
},
{
"street": "456 Oak Ave",
"city": "Shelbyville",
"@type": "secondary"
}
]
}
}
"#;
let xml_string = json_to_xml_with_root(&json_string, "People").unwrap();
println!("{}", xml_string);
```
## Expected Output (XML):
```xml
Springfield
123 Main St
Shelbyville
456 Oak Ave
30
John Doe
```
## Parameters:
- `json_string`: The input JSON string to be converted into XML.
- `root`: The name for the root element of the XML. It will become the root element of the XML if the JSON contains top-level @ attributes.
## Returns:
A `Result` which is either a `String` containing the XML representation of the input JSON, or a `ConversionError` if parsing or conversion fails.
## Notes:
- This function works recursively to handle nested structures and arrays.
- JSON keys starting with `@` are treated as attributes for the parent XML element.
- All XML element tags are automatically capitalized.
- Empty JSON objects (`{}`) are converted into self-closing tags (e.g., ``).
- Empty JSON arrays (`[]`) are converted into an empty element (e.g., ``).
- `null` values in JSON are converted into a self-closing `` tag.