Expand description

XML2JSON

A library for converting to and from XML and JSON.

JSON to XML

XmlBuilder

XmlBuilder builds a XML from JSON.

Example

use xml2json_rs::XmlBuilder;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
  let mut xml_builder = XmlBuilder::default();
  let xml= xml_builder.build_from_json_string(r#"{"book":{"$":{"category":"fantasy"},"title":[{"_":"The Name of the Wind","$":{"lang":"en"}}],"author":["Patrick Rothfuss"],"year":["2007"]}}"#)?;
  assert_eq!(xml, r#"<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title><author>Patrick Rothfuss</author><year>2007</year></book>"#);
  Ok(())
}

XmlConfig

XmlConfig Uses the Builder pattern to set configuration options and then finalize to build an XmlBuilder

Example

use xml2json_rs::XmlConfig;
use xml2json_rs::{ Indentation, Declaration, Version, Encoding };
use std::error::Error;
use indoc::indoc;

fn main() -> Result<(), Box<dyn Error>> {
  let mut xml_builder = XmlConfig::new()
    .rendering(Indentation::new(b' ', 2))
    .decl(Declaration::new(Version::XML10, Some(Encoding::UTF8), Some(true)))
    .attrkey("^")
    .root_name("store")
    .finalize();

  let xml = xml_builder.build_from_json_string(r#"{"book":{"^":{"category":"fantasy"},"title":[{"_":"The Name of the Wind","^":{"lang":"en"}}],"author":["Patrick Rothfuss"],"year":["2007"]}}"#)?;
  assert_eq!(xml, indoc!(r#"
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <store>
    <book category="fantasy">
      <title lang="en">The Name of the Wind</title>
      <author>Patrick Rothfuss</author>
      <year>2007</year>
    </book>
  </store>"#));
  Ok(())
}

XML to JSON

JsonBuilder

JsonBuilder builds JSON from XML.

Example

use xml2json_rs::JsonBuilder;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
  let json_builder = JsonBuilder::default();
  let json = json_builder.build_string_from_xml(r#"<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title><author>Patrick Rothfuss</author><year>2007</year></book>"#)?;
  assert_eq!(json, r#"{"book":{"$":{"category":"fantasy"},"title":[{"$":{"lang":"en"},"_":"The Name of the Wind"}],"author":["Patrick Rothfuss"],"year":["2007"]}}"#);
  Ok(())
}

JsonConfig

JsonConfig Uses the Builder pattern to set configuration options and then finalize to build an JsonBuilder

Example

use xml2json_rs::JsonConfig;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
  let json_builder = JsonConfig::new()
    .ignore_attrs(true)
    .explicit_array(false)
    .finalize();
  let json = json_builder.build_string_from_xml(r#"<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title><author>Patrick Rothfuss</author><year>2007</year></book>"#)?;
  assert_eq!(json, r#"{"book":{"title":"The Name of the Wind","author":"Patrick Rothfuss","year":"2007"}}"#);
  Ok(())
}

Structs

XML Declaration

XML Indentation rendering options

JSON builder struct for building JSON from XML

Configuration options for JsonBuilder

This type represents all possible errors that can occur when converting to or from XML and JSON

XML builder

XmlBuilder configuration options

Enums

XML Declaration encoding.

XML Declaration version.