XML是可扩展标记语言的简称,作为一种通用的数据交换格式,它的平台无关性、语言无关性、系统无关性 给数据集成与交互带来了极大的方便。XML在不同语言中的解析方式都是一样的,只不过实现的语言不同而已。
在Java中,XML的解析方式有四种,分别是:DOM解析;SAX解析;JDOM解析;DOM4J解析。前两种属于官方自带的解析方式,与平台无关;后两者是扩展方法,只适用于Java平台。
OK,这篇,我们只研究使用最广泛的DOM4J。
1、简介:
关于dom4j,首先我们要知道,它不是属于Java自带的解析XML方式,也就是说,要想使用dom4j,首先你需要先添加dom4j jar包。
OK,添加完jar包之后,我们就可以研究下它的语法了。
2、语法:
1)、获取根元素:
Element e1 = document.getRootElement();
2)、获取某个元素下面的子元素:
Element e2 = root.element("xxx");
3)、获取元素的子元素集合1 (所有子元素)
List<Element> e_list = e2.elements();
4)、获取元素的子元素集合2(指定名称的子元素集合)
List<Element> e_list = e2.elements("user");
5)、获取元素属性的值
elements.attributeValue("name");
6)、获取元素的内容
element.getText();
。。。
3、一个栗子:
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id="1"> <name>Java</name> <author>aaa</author> <year>2014</year> <price>89</price> </book> <book id="2"> <name>Linux</name> <author>bbb</author> <year>2015</year> <price>77</price> </book> <book id="3"> <name>Spring</name> <author>ccc</author> <year>2016</year> <price>100</price> </book> </bookstore> public class Book { private int id; private String name; private String author; private int year; private double price; //setter,getter ... @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price=" + price + "]"; } } public class Test09 { private List<Book> bookList = null; private Book book = null; private List<Book> getBooks(File file) { SAXReader reader = new SAXReader(); try { Document document = reader.read(file); Element bookstore = document.getRootElement(); Iterator stores = bookstore.elementIterator(); bookList = new ArrayList<Book>(); while(stores.hasNext()){ book = new Book(); Element bookElement = (Element) stores.next(); List<Attribute> attributes = bookElement.attributes(); for(Attribute attribute : attributes){ if(attribute.getName().equals("id")){ String id = attribute.getValue(); book.setId(Integer.parseInt(id)); } } Iterator bookit = bookElement.elementIterator(); while(bookit.hasNext()){ Element child = (Element) bookit.next(); String nodeName = child.getName(); if(nodeName.equals("name")){ String name = child.getStringValue(); book.setName(name); }else if(nodeName.equals("author")){ String author = child.getStringValue(); book.setAuthor(author); }else if(nodeName.equals("year")){ String year = child.getStringValue(); book.setYear(Integer.parseInt(year)); }else if(nodeName.equals("price")){ String price = child.getStringValue(); book.setName(price); } } bookList.add(book); book = null; } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bookList; } public static void main(String[] args) { File file = new File("src/java_19/book.xml"); List<Book> bookList = new Test09().getBooks(file); for(Book book:bookList){ System.out.println(book); } } }
控制台输出:
Book [id=1, name=89, author=aaa, year=2014, price=0.0] Book [id=2, name=77, author=bbb, year=2015, price=0.0] Book [id=3, name=100, author=ccc, year=2016, price=0.0]
OK,先这样,饿了 准备下班...
dom4j各种方法请参考:https://www.cnblogs.com/maxudong/p/8710291.html
java解析xml四种方法请参考:https://www.cnblogs.com/longqingyang/p/5577937.html
来源:https://www.cnblogs.com/Rain1203/p/10882746.html