Expand description
Parser and encoder for Android Binary XML (ABX).
ABX is the binary XML format written by AOSP’s BinaryXmlSerializer and read
by BinaryXmlPullParser. Android uses it for platform files such as
/data/system/packages.xml. It is not AXML, the format of compiled APK
resources such as AndroidManifest.xml, which this crate cannot read.
§Parsing
AbxParser reads a document from a byte slice and AbxStreamParser from
any Read. Both have the same methods and yield the same
Events.
use android_abx::{AbxParser, Event};
let mut parser = AbxParser::new(data)?;
while let Some(event) = parser.next_event()? {
if let Event::StartTag { name, attributes } = event {
println!("<{name}> has {} attributes", attributes.len());
}
}To convert a whole document to XML text, use abx_to_xml:
let xml = android_abx::abx_to_xml(data)?;
assert_eq!(
xml,
r#"<?xml version="1.0" encoding="UTF-8"?><pkg name="com.example.chat" version="3" flags="1"></pkg>"#,
);§Encoding
AbxWriter and events_to_abx encode Events back to ABX. With the
xml feature, xml_to_abx encodes XML text.
use android_abx::{AbxParser, events_to_abx};
let events = AbxParser::new(data)?.collect_events()?;
assert_eq!(events_to_abx(&events)?, data);§Deserializing with serde
With the serde feature, an element can be deserialized into any type
that implements serde::Deserialize. Struct fields are filled from:
- attributes, by name. Use
#[serde(rename = "...")]for names that are not Rust identifiers; - child elements, by tag name. A
Vec<T>field takes every matching child, any other field takes the first one. A child with only text can fill a scalar field such asStringoru32; - the element’s text, through a field renamed to
"$text". Only text events are collected: entity references such as&are dropped.
When an attribute and a child element have the same name, the attribute is
used. An Option field is None when the attribute or child is missing, or
when the attribute has a null value. Enums with unit variants are matched by
name against a string value.
Use from_slice, from_reader or from_file when the root element is the
record you want. For repeated elements under a root, such as <pkg> entries
in packages.xml, use AbxParser::deserialize_all or
AbxStreamParser::deserialize_iter.
§Feature flags
serde: serde deserialization.xml:xml_to_abx, to encode XML text.
Modules§
Structs§
- AbxParser
- A pull parser over an ABX document in memory.
- AbxParser
Owned - An ABX document that owns its bytes.
- AbxStream
Parser - A pull parser that reads an ABX document from any
Readsource. - AbxWriter
- Encodes
Events as ABX to anyWritesink. - Attribute
- An attribute: a name and a typed value.
Enums§
- AbxError
- The error type for every fallible operation in this crate.
- Attribute
Value - A typed attribute value.
- Event
- A parse event, one per token in the document.
Constants§
- MAGIC
- The 4-byte header that starts every ABX document:
ABX\0.
Functions§
- abx_
events - Parses an ABX document into a list of events.
- abx_
to_ xml - Converts an ABX document to an XML string.
- events_
to_ abx - Encodes a list of events as ABX bytes.
- from_
element serde - Deserializes
Tfrom an element’s attributes and optional text. - from_
file serde - Deserializes the root element of an ABX file into
T. - from_
reader serde - Deserializes the root element of an ABX document read from
readerintoT. - from_
slice serde - Deserializes the root element of an ABX document into
T. - open_
file - Opens a file and returns an
AbxStreamParserover it. - xml_
to_ abx xml - Encodes XML text as ABX bytes.
Type Aliases§
- Interned
Str - A tag or attribute name.
- Result
- A
ResultwithAbxErroras the error type.