Just An Application

August 20, 2009

A Standalone Android Runtime: XML Parsing

During development XML is used for specifying Android manifests and resources. Both manifests and resources are subsequently accessed by the runtime.

In the case of the SystemServer, it is actually the PackageManagerService which first accesses a manifest as it scans the framework directory (on a device this would be /system/framework but in our case isn’t) and encounters the



It creates an instance of android.content.res.PackageParser to parse the package, and this in turn calls the android.content.res.AssetManager method

    public final XmlResourceParser openXmlResourceParser(
                                       int    cookie, 
                                       String fileName) 



as the fileName argument.

This is a problem for us, because, yes you’ve guessed it, its a native method. Not much of a problem surely ? Java XML parsers are two a penny. True, but by the time the runtime encounters an XML file it is no longer in the de-facto XML format, it has in fact been transformed into a form of binary XML.

Fortunately the format is specified in the file


along with a bunch of other stuff which we will be making use of later on.

At this point the interesting stuff starts at line 467. To begin with its not entirely obvious what’s going on, but after a litle bit of experimentation it becomes clear.

For example, a start tag is represented by a

    struct ResXMLTree_node

followed by a

    struct ResXMLTree_attrExt

followed by zero or more

    struct ResXMLTree_attribute

so what it actually looks like in the file is

struct ResXMLTree_node

struct ResChunk_header

type (0x0102) uint16_t
headerSize uint16_t
size uint32_t
lineNumber uint32_t
comment uint32_t

struct ResXMLTree_attrExt

namespace uint32_t
name uint32_t
attributeStart uint16_t
attributeSize uint16_t
idAttributeIndex uint16_t
classAttributeIndex uint16_t
styleAttributeIndex uint16_t

struct ResXMLTree_attribute[0]

namespace uint32_t
name uint32_t
rawValue uint32_t
typedValue Value

Once the format is clear then the parser itself is straightforward.

Copyright (c) 2009 By Simon Lewis. All Rights Reserved.


Create a free website or blog at WordPress.com.

%d bloggers like this: