這篇文章將為大家詳細(xì)講解有關(guān)如何使用JDOM解析XML文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
在雙城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)網(wǎng)站建設(shè),雙城網(wǎng)站建設(shè)費(fèi)用合理。JDOM 提供了非常優(yōu)秀的Java XML API來(lái)更方便的讀取、修改、生成XML文檔。JDOM還提供了包裝類供用戶從SAX、DOM、STAX事件解析、STAX流解析中選擇具體的實(shí)現(xiàn)。
在本教程中,我們一起學(xué)習(xí)使用JDOM將讀取XML文件信息,并轉(zhuǎn)換為Java對(duì)象。
JDOM并非標(biāo)準(zhǔn)JDK的一部分,因此使用JDOM需要從官方網(wǎng)站中下載JDOM的二進(jìn)制包,下載后將JDOM的Jar包添加到工程的classpath中即可。
JDOM提供了包裝類供我們選擇底層的XML解析API,它提供了四個(gè)重要的類,我們可以使用它獲取JDOM的Document對(duì)象。JDOM Document對(duì)象提供非常有用的方法來(lái)獲取根元素,子元素列表,屬性值等。
JDOM的重要的類:org.jdom2.input.DOMBuilder:
使用DOM解析機(jī)制解析XML并將它轉(zhuǎn)換為JDOM Document對(duì)象。org.jdom2.input.SAXBuilder:
使用SAX解析機(jī)制解析XML并轉(zhuǎn)換為JDOM Document。org.jdom2.input.StAXEventBuilder
和org.jdom2.input.StAXStreamBuilder
作用和前面兩個(gè)類似,不再贅述。org.jdom2.Document
JDOM Document對(duì)象提供有用的方法獲取根元素,讀取或修改元素內(nèi)容等操作,我們將要使用它獲取XML的根元素。org.jdom2.Document
提供了有用的方法來(lái)獲取子元素集合,獲取子元素值,獲取屬性值等操作。
接下來(lái)我們開(kāi)始使用案例程序讀取XML文件并生成Java對(duì)象。
employees.xml
<?xml version="1.0" encoding="UTF-8"?><Employees> <Employee id="1"> <age>29</age> <name>Pankaj</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>35</age> <name>Lisa</name> <gender>Female</gender> <role>CEO</role> </Employee> <Employee id="3"> <age>40</age> <name>Tom</name> <gender>Male</gender> <role>Manager</role> </Employee></Employees>
該xml文件存放員工信息,我們使用Employee類表示員工。
package com.journaldev.xml;public class Employee { private int id; private String name; private String gender; private int age; private String role; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "Employee:: ID="+this.id+" Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender + " Role=" + this.role; } }
接著在測(cè)試程序中使用DOMBuilder讀取XML文件生成Employee對(duì)象集合。
JDOMXMLReader.java
package com.journaldev.xml.jdom; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.DOMBuilder; import org.jdom2.input.SAXBuilder; import org.jdom2.input.StAXEventBuilder; import org.jdom2.input.StAXStreamBuilder; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.journaldev.xml.Employee; public class JDOMXMLReader { public static void main(String[] args) { final String fileName = "/Users/pankaj/employees.xml"; org.jdom2.Document jdomDoc; try { //we can create JDOM Document from DOM, SAX and STAX Parser Builder classes jdomDoc = useDOMParser(fileName); Element root = jdomDoc.getRootElement(); List<Element> empListElements = root.getChildren("Employee"); List<Employee> empList = new ArrayList<>(); for (Element empElement : empListElements) { Employee emp = new Employee(); emp.setId(Integer.parseInt(empElement.getAttributeValue("id"))); emp.setAge(Integer.parseInt(empElement.getChildText("age"))); emp.setName(empElement.getChildText("name")); emp.setRole(empElement.getChildText("role")); emp.setGender(empElement.getChildText("gender")); empList.add(emp); } //lets print Employees list information for (Employee emp : empList) System.out.println(emp); } catch (Exception e) { e.printStackTrace(); } } //Get JDOM document from DOM Parser private static org.jdom2.Document useDOMParser(String fileName) throws ParserConfigurationException, SAXException, IOException { //creating DOM Document DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new File(fileName)); DOMBuilder domBuilder = new DOMBuilder(); return domBuilder.build(doc); } }
如你所見(jiàn),這里我使用DOM解析包裝類獲取JDOM Document對(duì)象。
運(yùn)行程序輸出:
Employee:: ID=1 Name=Pankaj Age=29 Gender=Male Role=Java DeveloperEmployee:: ID=2 Name=Lisa Age=35 Gender=Female Role=CEOEmployee:: ID=3 Name=Tom Age=40 Gender=Male Role=Manager
我們同樣可以使用SAX和STAX解析機(jī)制來(lái)完成,我們可以使用下面方法完成:
/Get JDOM document from SAX Parserprivate static org.jdom2.Document useSAXParser(String fileName) throws JDOMException, IOException { SAXBuilder saxBuilder = new SAXBuilder(); return saxBuilder.build(new File(fileName)); } //Get JDOM Document from STAX Stream Parser or STAX Event Parserprivate static org.jdom2. Document useSTAXParser(String fileName, String type) throws FileNotFoundException, XMLStreamException, JDOMException{ if(type.equalsIgnoreCase("stream")){ StAXStreamBuilder staxBuilder = new StAXStreamBuilder(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new FileInputStream(fileName)); return staxBuilder.build(xmlStreamReader); } StAXEventBuilder staxBuilder = new StAXEventBuilder(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName)); return staxBuilder.build(xmlEventReader); }
運(yùn)行程序?qū)?huì)得到相同的輸出,因?yàn)槲覀冎皇鞘褂貌煌陌b類,但是獲取的JDOM Document對(duì)象相同。
使用JDOM的好處是我們可以很方便的切換底層的解析機(jī)制而處理代碼不發(fā)生改變。
JDOM 提供了非常優(yōu)秀的Java XML API來(lái)更方便的讀取、修改、生成XML文檔。JDOM還提供了包裝類供用戶從SAX、DOM、STAX事件解析、STAX流解析中選擇具體的實(shí)現(xiàn)。
在本教程中,我們一起學(xué)習(xí)使用JDOM將讀取XML文件信息,并轉(zhuǎn)換為Java對(duì)象。
JDOM并非標(biāo)準(zhǔn)JDK的一部分,因此使用JDOM需要從官方網(wǎng)站中下載JDOM的二進(jìn)制包,下載后將JDOM的Jar包添加到工程的classpath中即可。
JDOM提供了包裝類供我們選擇底層的XML解析API,它提供了四個(gè)重要的類,我們可以使用它獲取JDOM的Document對(duì)象。JDOM Document對(duì)象提供非常有用的方法來(lái)獲取根元素,子元素列表,屬性值等。
JDOM的重要的類:org.jdom2.input.DOMBuilder:
使用DOM解析機(jī)制解析XML并將它轉(zhuǎn)換為JDOM Document對(duì)象。org.jdom2.input.SAXBuilder:
使用SAX解析機(jī)制解析XML并轉(zhuǎn)換為JDOM Document。org.jdom2.input.StAXEventBuilder
和org.jdom2.input.StAXStreamBuilder
作用和前面兩個(gè)類似,不再贅述。org.jdom2.Document
JDOM Document對(duì)象提供有用的方法獲取根元素,讀取或修改元素內(nèi)容等操作,我們將要使用它獲取XML的根元素。org.jdom2.Document
提供了有用的方法來(lái)獲取子元素集合,獲取子元素值,獲取屬性值等操作。
接下來(lái)我們開(kāi)始使用案例程序讀取XML文件并生成Java對(duì)象。
employees.xml
<?xml version="1.0" encoding="UTF-8"?><Employees> <Employee id="1"> <age>29</age> <name>Pankaj</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>35</age> <name>Lisa</name> <gender>Female</gender> <role>CEO</role> </Employee> <Employee id="3"> <age>40</age> <name>Tom</name> <gender>Male</gender> <role>Manager</role> </Employee></Employees>
該xml文件存放員工信息,我們使用Employee類表示員工。
package com.journaldev.xml; public class Employee { private int id; private String name; private String gender; private int age; private String role; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "Employee:: ID="+this.id+" Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender + " Role=" + this.role; } }
接著在測(cè)試程序中使用DOMBuilder讀取XML文件生成Employee對(duì)象集合。
JDOMXMLReader.java
package com.journaldev.xml.jdom; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.jdom2.Element;import org.jdom2.JDOMException; import org.jdom2.input.DOMBuilder; import org.jdom2.input.SAXBuilder; import org.jdom2.input.StAXEventBuilder; import org.jdom2.input.StAXStreamBuilder; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.journaldev.xml.Employee; public class JDOMXMLReader { public static void main(String[] args) { final String fileName = "/Users/pankaj/employees.xml"; org.jdom2.Document jdomDoc; try { //we can create JDOM Document from DOM, SAX and STAX Parser Builder classes jdomDoc = useDOMParser(fileName); Element root = jdomDoc.getRootElement(); List<Element> empListElements = root.getChildren("Employee"); List<Employee> empList = new ArrayList<>(); for (Element empElement : empListElements) { Employee emp = new Employee(); emp.setId(Integer.parseInt(empElement.getAttributeValue("id"))); emp.setAge(Integer.parseInt(empElement.getChildText("age"))); emp.setName(empElement.getChildText("name")); emp.setRole(empElement.getChildText("role")); emp.setGender(empElement.getChildText("gender")); empList.add(emp); } //lets print Employees list information for (Employee emp : empList) System.out.println(emp); } catch (Exception e) { e.printStackTrace(); } } //Get JDOM document from DOM Parser private static org.jdom2.Document useDOMParser(String fileName) throws ParserConfigurationException, SAXException, IOException { //creating DOM Document DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new File(fileName)); DOMBuilder domBuilder = new DOMBuilder(); return domBuilder.build(doc); } }
如你所見(jiàn),這里我使用DOM解析包裝類獲取JDOM Document對(duì)象。
運(yùn)行程序輸出:
Employee:: ID=1 Name=Pankaj Age=29 Gender=Male Role=Java DeveloperEmployee:: ID=2 Name=Lisa Age=35 Gender=Female Role=CEOEmployee:: ID=3 Name=Tom Age=40 Gender=Male Role=Manager
我們同樣可以使用SAX和STAX解析機(jī)制來(lái)完成,我們可以使用下面方法完成:
/Get JDOM document from SAX Parserprivate static org.jdom2.Document useSAXParser(String fileName) throws JDOMException, IOException { SAXBuilder saxBuilder = new SAXBuilder(); return saxBuilder.build(new File(fileName)); } //Get JDOM Document from STAX Stream Parser or STAX Event Parserprivate static org.jdom2.Document useSTAXParser(String fileName, String type) throws FileNotFoundException, XMLStreamException, JDOMException{ if(type.equalsIgnoreCase("stream")){ StAXStreamBuilder staxBuilder = new StAXStreamBuilder(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new FileInputStream(fileName)); return staxBuilder.build(xmlStreamReader); } StAXEventBuilder staxBuilder = new StAXEventBuilder(); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName)); return staxBuilder.build(xmlEventReader); }
運(yùn)行程序?qū)?huì)得到相同的輸出,因?yàn)槲覀冎皇鞘褂貌煌陌b類,但是獲取的JDOM Document對(duì)象相同。
使用JDOM的好處是我們可以很方便的切換底層的解析機(jī)制而處理代碼不發(fā)生改變。
關(guān)于如何使用JDOM解析XML文件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享名稱:如何使用JDOM解析XML文件-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://jinyejixie.com/article0/dhdpio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站策劃、全網(wǎng)營(yíng)銷推廣、ChatGPT、品牌網(wǎng)站設(shè)計(jì)、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容