使用dom4j解析xml文件
2017-09-12
目录
xml源文件
<?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Brand name="华为"> <Type name="P9"/> </Brand> <Brand name="苹果"> <Type name="iphone5"/> <Type name="iphone6"/> </Brand> </PhoneInfo>
代码:
import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.Iterator; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Dom4jTest { Document dom = null; public static void main(String[] args) { Dom4jTest dom4j = new Dom4jTest(); //获得Dom树 dom4j.getDom(); //添加新的节点 dom4j.addNode(); //更改元素属性 dom4j.modify(); //更改三星属性 dom4j.modifySan(); //删除节点 dom4j.delete(); //保存xml文件 dom4j.saveXml("手机信息.xml"); //显示xml文件 dom4j.showInfo(); } // 获得dom树 public void getDom() { //通过reader获得Dom树 SAXReader reader = new SAXReader(); try { dom = reader.read("收藏信息.xml"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 保存xml文件 public void saveXml(String path) { //设置编码格式 //可以直接使用无参构造,效果也一样 //OutputFormat encoding=new OutputFormat(); OutputFormat encoding = OutputFormat.createPrettyPrint(); encoding.setEncoding("gb2312"); Writer writer = null; try { writer = new FileWriter(path); //使用XMLWriter类进行dom树的输出 XMLWriter writerXml = new XMLWriter(writer, encoding); //使用Write()进行dom树的写 writerXml.write(dom); //最后关闭相关流 writerXml.close(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 显示xml文件信息 public void showInfo() { //获得dom树的根节点 Element BrandElements = dom.getRootElement(); //获得元素的Iterator集合 Iterator elementIterator = BrandElements.elementIterator(); //对Interator集合进行遍历 while (elementIterator.hasNext()) { Element brandElement = (Element) elementIterator.next(); //获得元素的属性 String brand = brandElement.attributeValue("name"); //继续从上一个节点获得下一节点Iterator集合 Iterator typeElements = brandElement.elementIterator(); while (typeElements.hasNext()) { Element typeElement = (Element) typeElements.next(); String type = typeElement.attributeValue("name"); System.out.println("手机:" + brand + ",型号" + type); } } } // 添加节点 public void addNode() { //获得根节点 Element Root = dom.getRootElement(); //添加Brand元素 Element brand = Root.addElement("Brand"); //为Brand元素添加属性 brand.addAttribute("name", "三星"); //继续在新加元素添加Type元素 Element type = brand.addElement("Type"); //为Type元素添加属性 type.addAttribute("name", "note"); } // 修改属性,添加ID属性 public void modify() { Element root = dom.getRootElement(); Iterator elementIterator = root.elementIterator(); int i = 0; while (elementIterator.hasNext()) { Element brand = (Element) elementIterator.next(); brand.addAttribute("id", (++i) + ""); } }
//修改三星为SAMSUNG public void modifySan() { Element rootElement = dom.getRootElement(); Iterator elementIterator = rootElement.elementIterator(); while (elementIterator.hasNext()) { Element brand = (Element) elementIterator.next(); if (brand.attributeValue("name").equals("三星")) { brand.addAttribute("name", "SAMSUNG"); } } }
// 删除SAMSUNG元素 public void delete() { Element rootElement = dom.getRootElement(); Iterator elementIterator = rootElement.elementIterator(); while (elementIterator.hasNext()) { Element brand = (Element) elementIterator.next(); if (brand.attributeValue("name").equals("SAMSUNG")) { brand.getParent().remove(brand); } } } }