4

I wrote a question that has not had much success: https://stackoverflow.com/questions/21296465/serialize-hashmap-as-xml-without-repeating-parent-elements. It occurs to me that perhaps I was asking the wrong question.

Is there a built-in Android class for creating XML non-continuously?

By non-continuously, I mean able to manipulate existing elements (add/remove children, etc). So far, I've found methods of creating XML (XmlSerializer, building Strings), but only in a continuous document.

Here is the pseudocode for what I am looking for:

[...]
//Note: Element is not a real class, but I'm guessing there will need to be a class that handles adding/removing other attributes, values, and other Elements.

//add necessary header for XML
xmldoc.startXML(); 

// this creates and returns an Element representing "<object></object>" 
Element rootNode = xmldoc.addElement("object", ""); 

//this inserts "<key>key1</key>" into "<object></object>" and returns itself
Element key1 = rootNode.addChildElement("key", "key1"); 
//this inserts "<value>value1</value>" into "<object></object>" but I don't care about setting it as a variable for later use.
rootNode.addChildElement("value", "value1");

[...]

//write the XML as a String/Stream/Something (called handler here).
xmldoc.flush(handler);

With some additional logic those functions could create the following XML

<object>
    <key>root</key>
    <object>
        <key>key1</key>
        <value>value1</value>
    </object>
    <object>
        <key>key2</key>
        <value>value2</value>
    </object>
    <object>
        <key>ns</key>
        <object>
            <key>key3</key>
            <value>value3</value>
        </object>
        <object>
            <key>key4</key>
            <value>value4</value>
        </object>
    </object>
</object>
Community
  • 1
  • 1
TCCV
  • 3,142
  • 4
  • 25
  • 30

2 Answers2

1

Indeed you have to stick with XML DOM processing, where the whole document is stored in RAM which allows you to read/write it's nodes and attributes randomly. XPath helps traversing the xml-tree

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Can I use XPath with DOM writing while still in memory? I thought that XPath was just for parsing, not writing. – TCCV Jan 25 '14 at 00:37
  • You can use XPath ONLY while in memory! XPath is intended for searching for specific nodes, not parsing. There are lot's of info and samples on that topic out there, for example here http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java – injecteer Jan 25 '14 at 00:45
  • Thanks. I didn't mean while in memory, I meant when decoding not encoding. It makes sense now, though. – TCCV Jan 25 '14 at 00:48
0

After starting to create my own classes I started googling again, I found what I was looking for. It's actually very similar to my pseudocode. The classes use org.w3c.dom and javax.xml.parsers.DocumentBuilder It is outlined at http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/.

[...]
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
[...]
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

//New Document
Document doc = docBuilder.newDocument();

// root element
Element rootElement = doc.createElement("object");
doc.appendChild(rootElement);

Element child = doc.createElement("object");
doc.appendChild(rootElement);

// key element
Element key = doc.createElement("key");
key.appendChild(doc.createTextNode("key1"));
child.appendChild(key);

// value element
Element value = doc.createElement("value");
value.appendChild(doc.createTextNode("value1"));
child.appendChild(value);

Creates:

<object>
    <key>root</key>
    <object>
        <key>key1</key>
        <value>value1</value>
    </object>
</object>
TCCV
  • 3,142
  • 4
  • 25
  • 30