dom4j
作者:互联网
<?xml version="1.0" encoding="UTF-8"?> <books> <book id="book1"> <name>三国演义</name> <price>99.88</price> </book> <book id="book2"> <name>西游记</name> <price>77.88</price> </book> <book id="book3"> <name>李白的诗</name> <price>88.88</price> </book> </books>
import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.util.Iterator; import java.util.List; public class TestDom01 { public static void main(String[] args) throws DocumentException { SAXReader saxReader = new SAXReader(); Document doc = saxReader.read("books.xml"); //得到根节点 Element root = doc.getRootElement(); System.out.println(root.getName());//root //遍历显示所有的数据 List<Element> list = root.elements("book"); for (Element book:list){ Attribute id = book.attribute("id"); System.out.println("id:"+id.getValue()); //迭代器 Iterator<Element> it = book.elementIterator(); while (it.hasNext()){ Element element = it.next(); System.out.println(element.getName()+":"+element.getText()); } System.out.println("----------------"); } } }
import org.dom4j.Attribute; 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; import java.io.FileWriter; import java.io.IOException; import java.util.List; public class TestDom02 { public static void main(String[] args) throws DocumentException, IOException { //需求:实现 删除 id="book3"这样的一个节点 /* <book id="book"> <name>李白的诗</name> <price>88.88</price> </book> */ //解析xml文件 SAXReader saxReader = new SAXReader(); Document doc = saxReader.read("books.xml"); //得到根节点 Element root = doc.getRootElement(); //找到相应的节点 List<Element> bookList = root.elements("book"); for (Element book:bookList){ Attribute id = book.attribute("id"); if ("book3".equals(id.getValue())){ book.getParent().remove(book);//删除 } } //保存到xml文件 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter("books.xml")); writer.write(doc); writer.close(); System.out.println("删除成功"); } }
import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import java.io.FileWriter; import java.io.IOException; import java.util.List; public class TestDom03 { public static void main(String[] args) throws DocumentException, IOException { //需求:实现 新增节点 /* <book id="book"> <name>李白的诗</name> <price>88.88</price> </book> */ //解析xml文件 SAXReader saxReader = new SAXReader(); Document doc = saxReader.read("books.xml"); //得到根节点 Element root = doc.getRootElement(); //新增节点 Element book = DocumentHelper.createElement("book"); book.addAttribute("id", "book3"); Element name = DocumentHelper.createElement("name");//创建节点 name.setText("李白的诗");//设置属性 book.add(name);//添加节点 Element price = DocumentHelper.createElement("price"); price.setText("88.88"); book.add(price); root.add(book); //保存到xml文件 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter("books.xml")); writer.write(doc); writer.close(); System.out.println("新增成功"); } }
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; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.List; public class TestDom04 { public static void main(String[] args) throws DocumentException, IOException { //需求 实现 修改 id="book3"这样的一个节点 李白的诗-》李清照的诗句 SAXReader saxReader = new SAXReader(); Document doc = saxReader.read("books.xml"); //得到根节点 Element root = doc.getRootElement(); System.out.println(root.getName());//root //遍历所有的数据 List<Element> list = root.elements("book"); for (Element book : list) { //找到对应的节点 if ("book3".equals(book.attribute("id").getValue())) { //迭代器 Iterator<Element> it = book.elementIterator(); while (it.hasNext()) { Element element = it.next(); //找到要修改的文本 if ("李白的诗".equals(element.getText())) { element.setText("李清照的诗"); System.out.println(element.getName() + ":" + element.getText()); break; } } break; } } //保存到xml文件 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter("books.xml")); writer.write(doc); writer.close(); System.out.println("修改成功"); } }
标签:dom4j,Element,book,io,org,import 来源: https://www.cnblogs.com/linlinmailbox/p/16514067.html