xml文件解析和序列化
Posted 凤雏小呆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml文件解析和序列化相关的知识,希望对你有一定的参考价值。
XmlPullParser parser = Xml.newPullParser(); //创建一个XmlPullParser实例 parser.setInput(is, "UTF-8"); //设置输入流 并指明编码方式 int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: books = new ArrayList<Book>(); break; case XmlPullParser.START_TAG: if (parser.getName().equals("book")) { book = new Book(); } else if (parser.getName().equals("id")) { eventType = parser.next(); book.setId(Integer.parseInt(parser.getText())); } else if (parser.getName().equals("name")) { eventType = parser.next(); book.setName(parser.getText()); } else if (parser.getName().equals("price")) { eventType = parser.next(); book.setPrice(Float.parseFloat(parser.getText())); } break; case XmlPullParser.END_TAG: if (parser.getName().equals("book")) { books.add(book); book = null; } break; } eventType = parser.next(); } return books; } @Override public String serialize(List<Book> books) throws Exception { // XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // XmlSerializer serializer = factory.newSerializer(); XmlSerializer serializer = Xml.newSerializer(); //创建一个XmlSerializer实例 StringWriter writer = new StringWriter(); serializer.setOutput(writer); //设置输出方向为writer serializer.startDocument("UTF-8", true); serializer.startTag("", "books"); for (Book book : books) { serializer.startTag("", "book"); serializer.attribute("", "id", book.getId() + ""); serializer.startTag("", "name"); serializer.text(book.getName()); serializer.endTag("", "name"); serializer.startTag("", "price"); serializer.text(book.getPrice() + ""); serializer.endTag("", "price"); serializer.endTag("", "book"); } serializer.endTag("", "books"); serializer.endDocument(); return writer.toString(); } }
转载:http://blog.csdn.net/liuhe688/article/details/6415593
- XmlPullParser parser = Xml.newPullParser(); //由android.util.Xml创建一个XmlPullParser实例
- parser.setInput(is, "UTF-8"); //设置输入流 并指明编码方式
- int eventType = parser.getEventType();
- while (eventType != XmlPullParser.END_DOCUMENT) {
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:
- books = new ArrayList<Book>();
- break;
- case XmlPullParser.START_TAG:
- if (parser.getName().equals("book")) {
- book = new Book();
- } else if (parser.getName().equals("id")) {
- eventType = parser.next();
- book.setId(Integer.parseInt(parser.getText()));
- } else if (parser.getName().equals("name")) {
- eventType = parser.next();
- book.setName(parser.getText());
- } else if (parser.getName().equals("price")) {
- eventType = parser.next();
- book.setPrice(Float.parseFloat(parser.getText()));
- }
- break;
- case XmlPullParser.END_TAG:
- if (parser.getName().equals("book")) {
- books.add(book);
- book = null;
- }
- break;
- }
- eventType = parser.next();
- }
- return books;
- }
- @Override
- public String serialize(List<Book> books) throws Exception {
- // XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- // XmlSerializer serializer = factory.newSerializer();
- XmlSerializer serializer = Xml.newSerializer(); //由android.util.Xml创建一个XmlSerializer实例
- StringWriter writer = new StringWriter();
- serializer.setOutput(writer); //设置输出方向为writer
- serializer.startDocument("UTF-8", true);
- serializer.startTag("", "books");
- for (Book book : books) {
- serializer.startTag("", "book");
- serializer.attribute("", "id", book.getId() + "");
- serializer.startTag("", "name");
- serializer.text(book.getName());
- serializer.endTag("", "name");
- serializer.startTag("", "price");
- serializer.text(book.getPrice() + "");
- serializer.endTag("", "price");
- serializer.endTag("", "book");
- }
- serializer.endTag("", "books");
- serializer.endDocument();
- return writer.toString();
- }
- }
以上是关于xml文件解析和序列化的主要内容,如果未能解决你的问题,请参考以下文章
GroovyXml 反序列化 ( 使用 XmlParser 解析 Xml 文件 | 删除 Xml 文件中的节点 | 增加 Xml 文件中的节点 | 将修改后的 Xml 数据输出到文件中 )