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

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

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

Continuously, I mean the ability to manipulate existing elements (add / remove child elements, etc.). So far I have found XML creation methods (XmlSerializer, build Strings), but only in a continuous document.

Here is the pseudo code for what I'm 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, these functions can 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>
+4
2

, , googling, , . . org.w3c.dom javax.xml.parsers.DocumentBuilder. 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);

:

<object>
    <key>root</key>
    <object>
        <key>key1</key>
        <value>value1</value>
    </object>
</object>
0

, XML DOM processing, , / . XPath XML-

+1

Source: https://habr.com/ru/post/1523407/


All Articles