XmlSerializer serializer = Xml.newSerializer();serializer.startTag(null,element);serializer.attribute(null,atbname,value);serializer.text(text);serializer.endTag(null,tag);
如果我想添加一个带有新元素,新属性等的新标签,我会在之前使用标签修改标签的位置输入元素.
如何将新标签附加到先前附加的标签?
解决方法 看一下 this link.它应该让您了解如何向XML添加节点.这是一个片段.public DomXMLExample() { try { ///////////////////////////// //Creating an empty XML document //We need a document documentBuilderFactory dbfac = documentBuilderFactory.newInstance(); documentBuilder docBuilder = dbfac.newdocumentBuilder(); document doc = docBuilder.newdocument(); //////////////////////// //Creating the XML tree //create the root element and add it to the document Element root = doc.createElement("root"); doc.appendChild(root); //create a comment and put it in the root element Comment comment = doc.createComment("Just a thought"); root.appendChild(comment); //create child element,add an attribute,and add to root Element child = doc.createElement("child"); child.setAttribute("name","value"); root.appendChild(child); //add a text element to the child Text text = doc.createTextNode("Filler,... I Could have had a foo!"); child.appendChild(text); ///////////////// //Output the XML //set up a transformer transformerFactory transfac = transformerFactory.newInstance(); transformer trans = transfac.newtransformer(); trans.setoutputProperty(OutputKeys.OMIT_XML_DECLaraTION,"yes"); trans.setoutputProperty(OutputKeys.INDENT,"yes"); //create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source,result); String xmlString = sw.toString(); //print xml System.out.println("Here's the xml:\n\n" + xmlString); } catch (Exception e) { System.out.println(e); } }总结
以上是内存溢出为你收集整理的如何在Android中附加XML中的标签?全部内容,希望文章能够帮你解决如何在Android中附加XML中的标签?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)