Skip to main content

Crate android_abx

Crate android_abx 

Source
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 as String or u32;
  • the element’s text, through a field renamed to "$text". Only text events are collected: entity references such as &amp; 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§

stream
Streaming parser over any Read source.

Structs§

AbxParser
A pull parser over an ABX document in memory.
AbxParserOwned
An ABX document that owns its bytes.
AbxStreamParser
A pull parser that reads an ABX document from any Read source.
AbxWriter
Encodes Events as ABX to any Write sink.
Attribute
An attribute: a name and a typed value.

Enums§

AbxError
The error type for every fallible operation in this crate.
AttributeValue
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_elementserde
Deserializes T from an element’s attributes and optional text.
from_fileserde
Deserializes the root element of an ABX file into T.
from_readerserde
Deserializes the root element of an ABX document read from reader into T.
from_sliceserde
Deserializes the root element of an ABX document into T.
open_file
Opens a file and returns an AbxStreamParser over it.
xml_to_abxxml
Encodes XML text as ABX bytes.

Type Aliases§

InternedStr
A tag or attribute name.
Result
A Result with AbxError as the error type.