pull解析和pull的写入
android常用的解析技术,javaee中使用起来也是非常方便的,轻便灵巧,下面我来为大家贴上代码演示pull的使用
首先是一个名为Product的javabean,后面的类是用来封装数据进javabean或者,将javabean数据写入到xml当中
public class Product {private String name;private double price;public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}} xml的解析和写入
package cn.itcast.pull;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import org.junit.Test;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserFactory;import org.xmlpull.v1.XmlSerializer;public class PULLCURDTest {// 删除商品@Test// 删除洗衣机public void testDelete() throws Exception {// 步骤一 将XML数据加载内存 List集合List<Product> products = parseXml2List("product.xml");// 步骤二 从内存List删除洗衣机for (Product product : products) {if (product.getName().equals("洗衣机")) {products.remove(product);break; // 只能删一个}}// 步骤三 将List集合回写XMLserializeList2Xml("product.xml", products);}// 测试修改商品@Test// 将产品价格提升20%public void testUpdate() throws Exception {// 步骤一 将XML数据加载内存 List集合List<Product> products = parseXml2List("product.xml");// 步骤二 修改内存中Listfor (Product product : products) {double price = product.getPrice();price = price * 1.2;// 上涨20%product.setPrice(price);}// 步骤三 将List集合回写XMLserializeList2Xml("product.xml", products);}// 测试增加商品@Testpublic void testAdd() throws Exception {// 步骤一 将XML数据加载内存 List集合List<Product> products = parseXml2List("product.xml");// 步骤二 添加商品Product product = new Product();product.setName("数码相机");product.setPrice(5000);products.add(product);// 步骤三 将List集合回写XMLserializeList2Xml("product.xml", products);}// 解析XML到Listpublic static List<Product> parseXml2List(String filename) throws Exception {List<Product> products = new ArrayList<Product>();// 解析xml 将xml数据 转换 Product对象,添加到集合XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();XmlPullParser parser = xmlPullParserFactory.newPullParser();parser.setInput(new FileInputStream(filename), "utf-8");int eventType;Product currentProduct = null;while ((eventType = parser.getEventType()) != XmlPullParser.END_DOCUMENT) {// 读取<product> 创建新的productif (eventType == XmlPullParser.START_TAG&& parser.getName().equals("product")) {currentProduct = new Product();}// 设置name属性if (eventType == XmlPullParser.START_TAG&& parser.getName().equals("name")) {currentProduct.setName(parser.nextText());}// 设置price属性if (eventType == XmlPullParser.START_TAG&& parser.getName().equals("price")) {currentProduct.setPrice(Double.parseDouble(parser.nextText()));}// 读取</product> 将对象加入集合if (eventType == XmlPullParser.END_TAG&& parser.getName().equals("product")) {products.add(currentProduct);currentProduct = null;}parser.next();}return products;}// 回写List到XMLpublic static void serializeList2Xml(String filename, List<Product> products)throws Exception {// 创建序列化对象XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();XmlSerializer serializer = parserFactory.newSerializer();serializer.setOutput(new FileOutputStream(filename), "utf-8");serializer.startDocument("utf-8", true);serializer.startTag(null, "products");for (Product product : products) {serializer.startTag(null, "product");serializer.startTag(null, "name");// name的值在product对象中serializer.text(product.getName());serializer.endTag(null, "name");serializer.startTag(null, "price");// price的值在product对象中serializer.text(product.getPrice() + "");serializer.endTag(null, "price");serializer.endTag(null, "product");}serializer.endTag(null, "products");serializer.endDocument();}}
页:
[1]