I'm planning on putting some XML files in my res/xml directory, which I want to load into Document objects (as I'm using these currently in my application and am passing them around a lot).
Doing:
XMLResourceParser parser = getResources().getXml(R.xml.my_xml_file)
seems to be the only starting point and the XMLResourceParser implements XmlPullParser, which seems to be aimed at iterating through the tree of nodes.
I've tried using xpath to pull out the root node of the document, as per the code in this answer: navigate through XML document with XMLpullParser, but this gives me an exception as per this question: Evaluating XPath an XmlResourceParser causes Exception
Ideally, I need to keep the xml in the res/xml folder, as I'll want to localise the content, so moving it to res/raw or res/assets isn't an option.
Before I implement something which iterates through the XmlResourceParser and builds the Document, is there a simpler way of doing this?
Writing the code to iterate through the
XMLResourceParser
shows that you are a nice well-mannered developer.However, you may be tempted to use the evil genius option.
First, create an
XmlPullParser
implementation that takes anXmlResourceParser
in the constructor, wraps it and delegates all its methods to it -- except all thesetInput
-type methods. You want all those to be no-op stubs, becauseXmlResourceParser
will throw exceptions (see XmlBlock.java, line 108 or so). Android studio can automate creating the delegate methods so you don't have to hand-code all that.Note: Your wrapper class might have to have some method implementations to handle turning off namespace processing and other assorted things.
Next, you will use a
org.xmlpull.v1.sax2.Driver
to wrap your parser and convert it into anXMLReader
:Now set up a dummy input source (the
XmlResourceParser
already knows where it's getting its input):Then you use a
Transformer
to convert your SAX input to a DOM output and get your result:MWAH Ha Ha Ha Ha Ha Ha!