简体   繁体   English

是否有用于非连续创建XML的内置Android类?

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

I wrote a question that has not had much success: https://stackoverflow.com/questions/21296465/serialize-hashmap-as-xml-without-repeating-parent-elements . 我写了一个没有取得多大成功的问题: 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? 是否有用于非连续创建XML的内置Android类?

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. 到目前为止,我已经找到了创建XML的方法(XmlSerializer,构建字符串),但仅限于连续文档。

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 通过一些额外的逻辑,这些函数可以创建以下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>

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. 实际上,您必须坚持使用XML DOM processing ,其中整个文档存储在RAM中,允许您随机读取/写入其节点和属性。 XPath helps traversing the xml-tree XPath有助于遍历xml-tree

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/ . 这些类使用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);

Creates: 创建:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM