/** * 遍历XML中的文件 */ public class XMLTest { public static void main(String[] args) throws Exception { //1、解析器工厂类 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2、解析器对象 DocumentBuilder builder = factory.newDocumentBuilder(); //3、指向硬盘文件,形成Document对象 Document document = builder.parse("src/books.xml"); // 4、 NodeList list = document.getElementsByTagName("book"); for(int i=0;i<list.getLength();i++){ Element book = (Element)list.item(i); String bookId = book.getAttribute("id"); String bookTypeId = book.getAttribute("type"); String bookName = book.getElementsByTagName("bookName").item(0).getFirstChild().getNodeValue(); String bookPrice = book.getElementsByTagName("bookPrice").item(0).getFirstChild().getNodeValue(); System.out.println(bookId+" "+bookTypeId+" "+bookName+" "+bookPrice); System.out.println(); } } }
/** * 向XML文件中增加元素,修改元素 * */ public class Test { public static void main(String[] args) throws Exception { addXml(); modifyXML(); } private static void modifyXML() throws Exception { // 1、构建工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 2、构建器 DocumentBuilder builder = factory.newDocumentBuilder(); // 3、parse(路径)解析哪个文件,幻化成内存中的一个对象,形态做了改变 Document document = builder.parse("src/books.xml"); // 4、获取子节点,一本一本图书 NodeList list = document.getElementsByTagName("book"); Element book = (Element) list.item(0); book.getElementsByTagName("bookName").item(0) .setTextContent("Html和CSS-谭浩强"); // 6.document write 硬盘上 // Transfer 传输、转移 内存---------->Disk硬盘上 TransformerFactory factory2 = TransformerFactory.newInstance(); Transformer transformer = factory2.newTransformer(); Source source = new DOMSource(document); Result result = new StreamResult("src/books.xml"); transformer.transform(source, result); System.out.println("success"); } public static void addXml() throws Exception { // 添加一本书,要求属性id type // 1、工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 2、解析器Builder DocumentBuilder builder = factory.newDocumentBuilder(); // 3、parese 解析出来Document Document document = builder.parse("src/books.xml"); // 4、在内存中构建出一个游离的Book对象 Element book = document.createElement("book"); book.setAttribute("id", "004"); book.setAttribute("type", "4"); Element bookName = document.createElement("bookName"); bookName.setTextContent("你是人间的四月天"); Element bookPrice = document.createElement("bookPrice"); bookPrice.setTextContent("86"); Element bookAuthor = document.createElement("bookAuthor"); bookAuthor.setTextContent("林徽因"); Element bookPublisher = document.createElement("bookPublisher"); bookPublisher.setTextContent("徐志摩出版社"); // 4.1 绑定 book.appendChild(bookName); book.appendChild(bookPrice); book.appendChild(bookAuthor); book.appendChild(bookPublisher); // 4.2将book对象和books绑定,作为他的子节点 // 5、将游离的book对象 隶属于books的子节点 document.getElementsByTagName("books").item(0).appendChild(book); // 6.document write 硬盘上 // Transfer 传输、转移 内存---------->Disk硬盘上 TransformerFactory factory2 = TransformerFactory.newInstance(); Transformer transformer = factory2.newTransformer(); Source source = new DOMSource(document); Result result = new StreamResult("src/books.xml"); transformer.transform(source, result); System.out.println("addOk"); } }
二、使用DOM4J遍历XML中的内容
public class MyDom4j { public static void main(String[] args) throws Exception { // 1、构建一个解析器 SAXReader reader = new SAXReader(); //2、获取到整个文档对象 Document document = reader.read("src/books.xml"); // 3、获取到根节点 Element root = document.getRootElement(); // 4、获取到根节点的子节点集合 List<Element> books = root.elements(); for(Element element:books){ // 5、一个element代表一本书 String bookName = element.element("bookName").getText(); System.out.println(bookName); } } }
三、使用DOM4J向XML中添加内容
public class MyMain { public static void main(String[] args) throws Exception { addXML(); } private static void addXML() throws Exception { SAXReader reader = new SAXReader(); //2、获取到整个文档对象 Document document = reader.read("src/books.xml"); //3、获取到根节点 Element root = document.getRootElement(); // 拿到了根节点 // 操作 Element book = DocumentHelper.createElement("book"); // 过时的方法 book.setAttributeValue("id", "005");
// 新方法 book.addAttribute("type","5"); //图书名称 Element bookName = DocumentHelper.createElement("bookName"); bookName.setText("资治通鉴"); // 关系,绑定一下 book.add(bookName); //将book和整个根节点绑定 root.add(book); // 将内存中有层级关系的节点放入到硬盘上 OutputStream os = new FileOutputStream("src/books.xml"); Writer writer = new OutputStreamWriter(os,"utf-8"); document.write(writer); writer.close(); System.out.println("写入成功!"); } }