PULL產生XML檔案,pull產生xml

來源:互聯網
上載者:User

PULL產生XML檔案,pull產生xml

 1 package xmlpulldemo; 2  3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6  7 import org.xmlpull.v1.XmlPullParserException; 8 import org.xmlpull.v1.XmlPullParserFactory; 9 import org.xmlpull.v1.XmlSerializer;10 11 public class PullProduceDocument {12 13     public static void main(String[] args) throws XmlPullParserException, IllegalArgumentException,14             IllegalStateException, FileNotFoundException, IOException {15 16         // 解析器工廠17         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();18         // 序列化器19         XmlSerializer serializer = factory.newSerializer();20         // 設定xml檔案的輸出位置21         serializer.setOutput(new FileOutputStream("src/books.xml"), "UTF-8");22         // 調用序列化器的相關方法向xml檔案中寫入資料23         serializer.startDocument("UTF-8", true);24         serializer.startTag(null, "books");25         for (int i = 0; i < 5; i++) {26             serializer.startTag(null, "book");27             serializer.attribute(null, "id", "book1");28             serializer.startTag(null, "name");29             serializer.text("三國演義");30             serializer.endTag(null, "name");31             serializer.startTag(null, "author");32             serializer.text("羅貫中");33             serializer.endTag(null, "author");34             serializer.startTag(null, "price");35             serializer.text("30$");36             serializer.endTag(null, "price");37             serializer.endTag(null, "book");38         }39         serializer.endTag(null, "books");40         serializer.endDocument();41 42     }43 44 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.