用DOM4J操作XML

    xiaoxiao2024-12-12  17

    创建XML文件:文件名:收藏信息 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <PhoneInfo> <Brand name="华为"> <Type name="HW2"/> </Brand> <Brand name="苹果"> <Type name="iPhone6"/> <Type name="iPhone6s"/> <Type name="iPhone7"/> </Brand> <Brand name="三星"> <Type name="NOTE2"/> </Brand> </PhoneInfo> 创建Java文件 import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class DOM4JXMLDemo { private Document document; //获得document对象 public void loadDocument() { SAXReader saxReader = new SAXReader(); try { document = saxReader.read(new File("收藏信息.xml")); } catch (DocumentException e) { e.printStackTrace(); } } //显示手机的品牌及型号 @SuppressWarnings("rawtypes") public void showPhoneInfo() { //获取XML根节点 Element root = document.getRootElement(); //获取所有Brand Iterator eleBrand = root.elementIterator(); while(eleBrand.hasNext()) { Element brand = (Element)eleBrand.next(); System.out.println(brand.attributeValue("name"));//打印所有的Brand Iterator eleType = brand.elementIterator(); while(eleType.hasNext()) { Element type = (Element)eleType.next(); System.out.println("\t"+type.attributeValue("name"));//打印所有的Type } } } //增加手机品牌 public void addNewPhone() { //获得XML根元素 Element root = document.getRootElement(); //创建Brand Element eleBrand = root.addElement("Brand"); eleBrand.addAttribute("name", "华为"); //创建<Type name="HW123"/> Element eleType = eleBrand.addElement("Type"); eleType.addAttribute("name", "HW123"); saveXML("newInfo.xml"); } //保存修改到XML文件 public void saveXML(String path) {//设置漂亮的格式 OutputFormat format = OutputFormat.createPrettyPrint(); // format.setEncoding("GBK");//设置保存的格式 XMLWriter writer = null; try {//输出流 writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)),format); writer.write(document); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } //修改节点 @SuppressWarnings("rawtypes") public void updataPhone() { //获得XML根元素 Element root = document.getRootElement(); Iterator eleBrand = root.elementIterator(); int id=0; while(eleBrand.hasNext()) { Element brand = (Element)eleBrand.next(); id++; brand.addAttribute("id", id+""); } saveXML("newInfo.xml"); } //删除节点 @SuppressWarnings("rawtypes") public void deletePhone() { //获得XML根元素 Element root = document.getRootElement(); Iterator eleBrand = root.elementIterator();//迭代器获取每个Brand while(eleBrand.hasNext()) { Element brand = (Element)eleBrand.next(); if(brand.attributeValue("name").equals("三星")) { brand.getParent().remove(brand);//拿到父节点用父节点删除子节点 } } saveXML("newInfo.xml"); } public static void main(String[] args) { DOM4JXMLDemo domDemo = new DOM4JXMLDemo(); domDemo.loadDocument(); domDemo.addNewPhone(); domDemo.updataPhone(); domDemo.deletePhone(); domDemo.showPhoneInfo(); } }
    最新回复(0)