private String writeXml() { XmlSerializer serializer = Xml.newSerializer(); StringWriter writer = new StringWriter(); try { serializer.setOutput(writer); // <?xml version="1.0″ encoding="UTF-8″ standalone="yes"?> serializer.startDocument("UTF-8", true); // <blog number="1″> serializer.startTag("", "blog"); serializer.attribute("", "number", String.valueOf(1)); // <message data="2009-09-23″> serializer.startTag("", "message"); serializer.attribute("", "date", "2009-09-23"); // <title>Android XML</title> serializer.startTag("", "title"); serializer.text("Android XML"); serializer.endTag("", "title"); // <url>http://developer.android.com//url> serializer.startTag("", "url"); serializer.text("http://developer.android.com/"); serializer.endTag("", "url"); // </message> serializer.endTag("", "message"); // </blog> serializer.endTag("", "blog"); serializer.endDocument(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return writer.toString(); }
XmlSerializer写XML
最新推荐文章于 2024-08-13 14:18:30 发布