XML : 可扩展标记语言,用来传输和存储数据。
DOM解析
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建dom解析工厂
DocumentBuilder db = dbf.newDocumentBuilder();//获得具体的dom解析器
Document document = db.parse(new File("xml/books.xml")); // 解析xml文件 , 获得document 对象
实例:
/**
* XML字符串流转对象
*/
public List<Books> parse(InputStream is) throws Exception {
List<Books> listbook=new ArrayList<Books> ();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建dom解析工厂
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("xml/books.xml")); // 解析xml文件 , 获得document 对象
NodeList list = document.getElementsByTagName("book"); // 获得 book节点的集合
for (int i = 0; i < list.getLength(); i++) {
Books book = new Books();
Node node = list.item(i);
NodeList cNodeList = node.getChildNodes(); // 获得 当前book下的所有节点
for (int j = 0; j < cNodeList.getLength(); j++) { // 遍历当前book下的所有节点
Node cnode = cNodeList.item(j);
if (cnode.getNodeType() == Node.ELEMENT_NODE) { // 为元素节点的时候
String content = cnode.getFirstChild().getTextContent(); // 获取当前节点的文本内容
if (cnode.getNodeName().equals("id")) {
book.setId(Integer.parseInt(content));
} else if (cnode.getNodeName().equals("name")) {
book.setName(content);
} else if (cnode.getNodeName().equals("price")) {
book.setPrice(Double.parseDouble(content));
}
}
}
listbook.add(book);
}
return listbook;
}
@Override
/**
* 对象转xml字符串
*/
public String serialize(List<Books> books) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建dom解析工厂
DocumentBuilder db = dbf.newDocumentBuilder();//获得具体的dom解析器
Document doc=db.newDocument();
Element rootElement = doc.createElement("books"); //创建books节点
for(Books blist :books){
Element bookElement = doc.createElement("book"); //创建book节点
bookElement.setAttribute("id", blist.getId() + ""); //创建属性节点
Element idElement = doc.createElement("id");
idElement.setTextContent(String.valueOf(blist.getId())); //转为字符串
bookElement.appendChild(idElement);
Element nameElement = doc.createElement("name"); //创建name节点
nameElement.setTextContent(blist.getName()); //设置name节点的文本内容
bookElement.appendChild(nameElement); //把name节点添加到book节点中
Element priceElement = doc.createElement("price");
priceElement.setTextContent(blist.getPrice()+"");
bookElement.appendChild(priceElement);
rootElement.appendChild(bookElement);
}
doc.appendChild(rootElement);
TransformerFactory transFactory = TransformerFactory.newInstance();// 取得TransformerFactory实例
Transformer transformer = transFactory.newTransformer(); // 从transFactory获取Transformer实例
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 设置输出采用的编码方式
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);// 表明目标结果为writer
Source source = new DOMSource(doc); // 表明文档来源是doc
transformer.transform(source, result);
return writer.toString();
}
DOM4J解析
DOM4J需要下载jar包---网站:https://dom4j.github.io/
实例:
/**
* XML解析成java对象
*/
public List<Book> xml_java() throws ParserConfigurationException, SAXException, IOException {
List<Book> booklist = new ArrayList<>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建dom解析工厂
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("xml/book.xml")); // 解析xml文件 , 获得document 对象
NodeList list = document.getElementsByTagName("book"); // 获得 book节点的集合
for (int i = 0; i < list.getLength(); i++) {
Book book = new Book();
Node node = list.item(i);
NodeList cNodeList = node.getChildNodes(); // 获得 当前book下的所有节点
for (int j = 0; j < cNodeList.getLength(); j++) { // 遍历当前book下的所有节点
Node cnode = cNodeList.item(j); //
if (cnode.getNodeType() == cnode.ELEMENT_NODE) { // 为元素节点的时候
String content = cnode.getFirstChild().getTextContent(); // 获取当前节点的文本内容
if (cnode.getNodeName().equals("id")) {
book.setId(Integer.parseInt(content));
} else if (cnode.getNodeName().equals("name")) {
book.setName(content);
} else if (cnode.getNodeName().equals("price")) {
book.setPrice(Double.parseDouble(content));
}
}
}
booklist.add(book);
}
return booklist;
}
/**
* java对象转XML文件
*/
public String java_xml(List<Book> book) throws ParserConfigurationException, TransformerException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建dom解析工厂
DocumentBuilder db = dbf.newDocumentBuilder();//获得具体的dom解析器
Document doc=db.newDocument();
Element rootElement = doc.createElement("books"); //创建books节点
for(Book blist :book){
Element bookElement = doc.createElement("book"); //创建book节点
bookElement.setAttribute("id", blist.getId() + ""); //创建属性节点
Element idElement = doc.createElement("id");
idElement.setTextContent(String.valueOf(blist.getId())); //转为字符串
bookElement.appendChild(idElement);
Element nameElement = doc.createElement("name"); //创建name节点
nameElement.setTextContent(blist.getName()); //设置name节点的文本内容
bookElement.appendChild(nameElement); //把name节点添加到book节点中
Element priceElement = doc.createElement("price");
priceElement.setTextContent(blist.getPrice()+"");
bookElement.appendChild(priceElement);
rootElement.appendChild(bookElement);
}
doc.appendChild(rootElement);
TransformerFactory transFactory = TransformerFactory.newInstance();// 取得TransformerFactory实例
Transformer transformer = transFactory.newTransformer(); // 从transFactory获取Transformer实例
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 设置输出采用的编码方式
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);// 表明目标结果为writer
Source source = new DOMSource(doc); // 表明文档来源是doc
transformer.transform(source, result);
return writer.toString();
}