是否有一个内置Android类非连续创建XML? [英] Is there a built-in Android class for creating XML non-continuously?
问题描述
的我写道,一直没有很大的成功一个问题:<一href=\"http://stackoverflow.com/questions/21296465/serialize-hashmap-as-xml-without-repeating-parent-elements\">Serialize HashMap中作为XML(不重复父元素)。它发生,我认为也许我是问错了问题。的
是否有一个内置Android类非连续创建XML?
通过非不断,我的意思是能够操纵现有元素(添加/删除儿童等)。到目前为止,我已经找到了,但只有在一个连续的文档创建XML(XmlSerializer的,建筑字串)的方法。
下面是伪code为我所期待的:
[...]
//注意:元素是不是一个真正的类,但我猜有将需要处理添加/删除其他属性,价值观,和其他元素的类。//添加对XML必要的头
xmldoc.startXML();//这将创建并返回一个元素重新presenting&LT;对象&gt;&LT; /对象&gt;
元素rootNode中= xmldoc.addElement(对象,);//这个插入&LT;密钥GT; key1的&LT; /键&gt;中进入&LT;对象&gt;&LT; /对象&gt;并返回本身
元素键1 = rootNode.addChildElement(键,KEY1);
//这个插入&LT; VALUE&GT;&VALUE1 LT; /值&gt;中进入&LT;对象&gt;&LT; /对象&gt;但我不关心它设置为供以后使用的变量。
rootNode.addChildElement(价值,值1);[...]//写XML作为字符串/流/东西(这里称为处理函数)。
xmldoc.flush(处理);
使用一些额外的逻辑,这些功能可以创建以下XML
&LT;对象&gt;
&LT;密钥GT;根&LT; /键&GT;
&LT;对象&gt;
&LT;密钥GT; key1的&LT; /键&GT;
&LT; VALUE&GT;&VALUE1 LT; /值&GT;
&LT; /对象&gt;
&LT;对象&gt;
&LT;密钥GT;&KEY2 LT; /键&GT;
&LT; VALUE&GT;&VALUE2 LT; /值&GT;
&LT; /对象&gt;
&LT;对象&gt;
&LT;密钥GT; NS&LT; /键&GT;
&LT;对象&gt;
&LT;密钥GT;&KEY3 LT; /键&GT;
&LT; VALUE&GT;&VALUE3 LT; /值&GT;
&LT; /对象&gt;
&LT;对象&gt;
&LT;密钥GT;&KEY4 LT; /键&GT;
&LT; VALUE&GT;&VALUE4 LT; /值&GT;
&LT; /对象&gt;
&LT; /对象&gt;
&LT; /对象&gt;
开始创建自己的班,我开始再次google搜索后,我找到了我一直在寻找。它实际上非常相似,我的伪code。这些类使用的org.w3c.dom javax.xml.parsers.DocumentBuilder中和在它的 http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ 。
[...]
进口javax.xml.parsers.DocumentBuilder中;
进口javax.xml.parsers.DocumentBuilderFactory中;进口org.w3c.dom.Document中;
进口org.w3c.dom.Element中;
[...]
的DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
的DocumentBuilder docBuilder = docFactory.newDocumentBuilder();//新建文档
文档的文档= docBuilder.newDocument();//根元素
元素rootElement的= doc.createElement(对象);
doc.appendChild(rootElement的);子元素= doc.createElement(对象);
doc.appendChild(rootElement的);//关键要素
元素键= doc.createElement(钥匙);
key.appendChild(doc.createTextNode(KEY1));
child.appendChild(键);//值元素
元素值= doc.createElement(值);
value.appendChild(doc.createTextNode(值1));
child.appendChild(值);
创建
&LT;对象&gt;
&LT;密钥GT;根&LT; /键&GT;
&LT;对象&gt;
&LT;密钥GT; key1的&LT; /键&GT;
&LT; VALUE&GT;&VALUE1 LT; /值&GT;
&LT; /对象&gt;
&LT; /对象&gt;
I wrote a question that has not had much success: 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>
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>
这篇关于是否有一个内置Android类非连续创建XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!