dom4j生成/解析xml檔案
阿新 • • 發佈:2019-01-01
解析
1.引入依賴
<!-- dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
2.需要解析的xml檔案
<?xml version="1.0" encoding="UTF-8"?> <books> <book id="imooc-001" category="歷史"> <name>三國演義</name> <author>羅貫中</author> <price>1030</price> </book> <book id="imooc-002" category="程式設計"> <name>Oracle開發實戰經典</name> <author>李興華</author> <price>60</price> </book> </books>
3.該xml檔案所對應的JavaBean
public class Book {
private String id;
private String category;
private String name;
private String author;
private int price;
//get和set方法
}
4.編寫讀取Dom物件的工具類Dom4jReaderUtils
public class Dom4jReaderUtils { public static Document getDocument() { String resource = "/Volumes/TOSHIBA EXT/1.學習/程式碼/6.SSM/springmvc/springMVC_demo/src/main/resources/file/books.xml"; Document document = null; SAXReader reader = new SAXReader(); try { document = reader.read(new File(resource)); } catch (DocumentException e) { e.printStackTrace(); } return document; } }
5.編寫寫入DOM物件的工具類Dom4jWriterUtils
//編寫寫入DOM物件的工具類Dom4jWriterUtils public class Dom4jWriterUtils { public static void writeDom(Document document) throws IOException { //建立檔案輸出的時候,自動縮排的格式 OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = null; try { //設定編碼 format.setEncoding("UTF-8"); //建立XMLWriter物件的時候,為防止亂碼最好傳入FileOutStream作為引數 writer = new XMLWriter(new FileOutputStream(new File("/Volumes/TOSHIBA EXT/1.學習/程式碼/6.SSM/springmvc/springMVC_demo/src/main/resources/file/Mapper.xml")), format); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
6.編寫Dom4jCRUD實現對books.xml進行讀取並進行測試
public class Dom4jCURD {
public static void main(String[] args) {
//讀取books檔案中的內容
Document document = Dom4jReaderUtils.getDocument();
List<Book> bookList=getBookListFromXML(document);
Iterator<Book> iterator = bookList.iterator();
while(iterator.hasNext()){
Book book = iterator.next();
System.out.println("id:"+book.getId());
System.out.println("category:"+book.getCategory());
System.out.println("author:"+book.getAuthor());
System.out.println("name:"+book.getName());
System.out.println("price:"+book.getPrice());
}
}
/**
* 讀取xml檔案中所有的book子節點並將其封裝成Book物件放在List中返回。
* @param document XML文件物件
* @return 存放Book物件的List
*/
public static List<Book> getBookListFromXML(Document document) {
List<Book> bookList = new ArrayList<Book>();
Element root = document.getRootElement();
Iterator<Element> bookIterator = root.elementIterator();
while (bookIterator.hasNext()) {
Element element = bookIterator.next();
Book book = new Book();
book.setName(element.elementText("name"));
book.setPrice(Integer.parseInt(element.elementText("price")));
book.setAuthor(element.elementText("author"));
Iterator<Attribute> bookAttr = element.attributeIterator();
while (bookAttr.hasNext()) {
Attribute attribute = bookAttr.next();
String attributeName = attribute.getName();
if (attributeName.equals("id")) {
book.setId(attribute.getValue());
} else {
book.setCategory(attribute.getValue());
}
}
bookList.add(book);
}
return bookList;
}
}
生成
1.引入依賴
<!-- dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
2.編寫生成xml檔案內容的程式碼
class Dom4JTest {
public static void main(String[] args) throws FileNotFoundException {
createXMLFile();
}
/**
*使用Dom4J建立一個新的XML文件
*/
public static void createXMLFile() throws FileNotFoundException {
//1.建立一個xml文件
Document document = DocumentHelper.createDocument();
//2.建立一個根元素,併為其設定屬性
Element root = DocumentHelper.createElement("mapper");
root.addAttribute("namespace","com.steven.ssm.mapper.ItemsMapperCustom");
//3.為根元素建立子節點,並設定屬性和值
document.setRootElement(root);
Element select = DocumentHelper.createElement("select");
select.addAttribute("id","getItemsList").addAttribute("resultMap","queryItems").addText("select * from items");
root.add(select);
Element resultMap = DocumentHelper.createElement("resultMap");
resultMap.addAttribute("id","queryItems").addAttribute("type","items");
root.add(resultMap);
Element id = resultMap.addElement("id");
id.addAttribute("column","item_id").addAttribute("property","itemId");
Element itemName = resultMap.addElement("itemName");
itemName.addAttribute("column","item_name").addAttribute("property","itemName");
//4.將文件內容寫入指定的xml檔案中
Dom4jWriterUtils.writeDom(document);
}
}
3.生成的xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.steven.ssm.mapper.ItemsMapperCustom">
<select id="getItemsList" resultMap="queryItems">select * from items</select>
<resultMap id="queryItems" type="items">
<id column="item_id" property="itemId"/>
<itemName column="item_name" property="itemName"/>
</resultMap>
</mapper>