package xmlpulldemo; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer; public class PullProduceDocument { public static void main(String[] args) throws XmlPullParserException, IllegalArgumentException,
IllegalStateException, FileNotFoundException, IOException { // 解析器工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 序列化器
XmlSerializer serializer = factory.newSerializer();
// 设置xml文件的输出位置
serializer.setOutput(new FileOutputStream("src/books.xml"), "UTF-8");
// 调用序列化器的相关方法向xml文件中写入数据
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "books");
for (int i = 0; i < 5; i++) {
serializer.startTag(null, "book");
serializer.attribute(null, "id", "book1");
serializer.startTag(null, "name");
serializer.text("三国演义");
serializer.endTag(null, "name");
serializer.startTag(null, "author");
serializer.text("罗贯中");
serializer.endTag(null, "author");
serializer.startTag(null, "price");
serializer.text("30$");
serializer.endTag(null, "price");
serializer.endTag(null, "book");
}
serializer.endTag(null, "books");
serializer.endDocument(); } }