java sax解析xml_在Java中使用DOM,SAX和StAX解析器解析XML
java sax解析xml
我碰巧通讀了有關Java中XML解析和構建API的章節。 我試用了樣本XML上的其他解析器。 然后,我想在我的博客上分享它,這樣我就可以得到該代碼的參考以及任何閱讀此代碼的參考。 在本文中,我將在不同的解析器中解析相同的XML,以執行將XML內容填充到對象中,然后將對象添加到列表中的相同操作。
示例中考慮的示例XML是:
<employees><employee id="111"><firstName>Rakesh</firstName><lastName>Mishra</lastName><location>Bangalore</location></employee><employee id="112"><firstName>John</firstName><lastName>Davis</lastName><location>Chennai</location></employee><employee id="113"><firstName>Rajesh</firstName><lastName>Sharma</lastName><location>Pune</location></employee> </employees>XML內容要提取到的對象定義如下:
class Employee{String id;String firstName;String lastName;String location;@Overridepublic String toString() {return firstName+" "+lastName+"("+id+")"+location;} }我提供了3個主要解析器的示例代碼:
- DOM解析器
- SAX解析器
- StAX解析器
使用DOM解析器
我正在使用JDK附帶的DOM解析器實現,在我的示例中,我使用的是JDK7。DOM解析器將完整的XML內容加載到Tree結構中。 然后,我們遍歷Node和NodeList以獲取XML的內容。 下面給出了使用DOM解析器進行XML解析的代碼。
public class DOMParserDemo {public static void main(String[] args) throws Exception {//Get the DOM Builder FactoryDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//Get the DOM BuilderDocumentBuilder builder = factory.newDocumentBuilder();//Load and Parse the XML document//document contains the complete XML as a Tree.Document document = builder.parse(ClassLoader.getSystemResourceAsStream("xml/employee.xml"));List<Employee> empList = new ArrayList<>();//Iterating through the nodes and extracting the data.NodeList nodeList = document.getDocumentElement().getChildNodes();for (int i = 0; i < nodeList.getLength(); i++) {//We have encountered an <employee> tag.Node node = nodeList.item(i);if (node instanceof Element) {Employee emp = new Employee();emp.id = node.getAttributes().getNamedItem("id").getNodeValue();NodeList childNodes = node.getChildNodes();for (int j = 0; j < childNodes.getLength(); j++) {Node cNode = childNodes.item(j);//Identifying the child tag of employee encountered. if (cNode instanceof Element) {String content = cNode.getLastChild().getTextContent().trim();switch (cNode.getNodeName()) {case "firstName":emp.firstName = content;break;case "lastName":emp.lastName = content;break;case "location":emp.location = content;break;}}}empList.add(emp);}}//Printing the Employee list populated.for (Employee emp : empList) {System.out.println(emp);}} }class Employee{String id;String firstName;String lastName;String location;@Overridepublic String toString() {return firstName+" "+lastName+"("+id+")"+location;} }上面的輸出將是:
Rakesh Mishra(111)Bangalore John Davis(112)Chennai Rajesh Sharma(113)Pune使用SAX解析器
SAX解析器不同于DOM解析器,在DOM解析器中,SAX解析器不會將完整的XML加載到內存中,而是在遇到不同元素(例如,打開標簽,關閉標簽,字符數據)時逐行觸發不同事件來解析XML ,評論等。 這就是SAX解析器被稱為基于事件的解析器的原因。
除了XML源文件,我們還注冊了擴展DefaultHandler類的處理程序。 DefaultHandler類提供了我們感興趣的各種回調:
- startElement() –遇到標簽的開始時觸發此事件。
- endElement() –遇到標簽結尾時觸發此事件。
- character() –遇到一些文本數據時觸發此事件。
下面給出了使用SAX Parser解析XML的代碼:
import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;public class SAXParserDemo {public static void main(String[] args) throws Exception {SAXParserFactory parserFactor = SAXParserFactory.newInstance();SAXParser parser = parserFactor.newSAXParser();SAXHandler handler = new SAXHandler();parser.parse(ClassLoader.getSystemResourceAsStream("xml/employee.xml"), handler);//Printing the list of employees obtained from XMLfor ( Employee emp : handler.empList){System.out.println(emp);}} } /*** The Handler for SAX Events.*/ class SAXHandler extends DefaultHandler {List<Employee> empList = new ArrayList<>();Employee emp = null;String content = null;@Override//Triggered when the start of tag is found.public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {switch(qName){//Create a new Employee object when the start tag is foundcase "employee":emp = new Employee();emp.id = attributes.getValue("id");break;}}@Overridepublic void endElement(String uri, String localName, String qName) throws SAXException {switch(qName){//Add the employee to list once end tag is foundcase "employee":empList.add(emp); break;//For all other end tags the employee has to be updated.case "firstName":emp.firstName = content;break;case "lastName":emp.lastName = content;break;case "location":emp.location = content;break;}}@Overridepublic void characters(char[] ch, int start, int length) throws SAXException {content = String.copyValueOf(ch, start, length).trim();}}class Employee {String id;String firstName;String lastName;String location;@Overridepublic String toString() {return firstName + " " + lastName + "(" + id + ")" + location;} }上面的輸出將是:
Rakesh Mishra(111)Bangalore John Davis(112)Chennai Rajesh Sharma(113)Pune使用StAX解析器
StAX代表XML的Streaming API,并且StAX Parser與DOM有所不同,就像SAX Parser一樣。 StAX解析器與SAX解析器也有細微的區別。
- SAX解析器推送數據,但StAX解析器從XML提取所需的數據。
- StAX解析器將光標保持在文檔的當前位置,從而可以提取光標處可用的內容,而SAX解析器在遇到某些數據時發出事件。
XMLInputFactory和XMLStreamReader是兩個可用于加載XML文件的類。 當我們使用XMLStreamReader讀取XML文件時,將以整數值的形式生成事件,然后將這些事件與XMLStreamConstants中的常量進行比較。 以下代碼顯示了如何使用StAX解析器解析XML:
import java.util.ArrayList; import java.util.List; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader;public class StaxParserDemo {public static void main(String[] args) throws XMLStreamException {List<Employee> empList = null;Employee currEmp = null;String tagContent = null;XMLInputFactory factory = XMLInputFactory.newInstance();XMLStreamReader reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream("xml/employee.xml"));while(reader.hasNext()){int event = reader.next();switch(event){case XMLStreamConstants.START_ELEMENT: if ("employee".equals(reader.getLocalName())){currEmp = new Employee();currEmp.id = reader.getAttributeValue(0);}if("employees".equals(reader.getLocalName())){empList = new ArrayList<>();}break;case XMLStreamConstants.CHARACTERS:tagContent = reader.getText().trim();break;case XMLStreamConstants.END_ELEMENT:switch(reader.getLocalName()){case "employee":empList.add(currEmp);break;case "firstName":currEmp.firstName = tagContent;break;case "lastName":currEmp.lastName = tagContent;break;case "location":currEmp.location = tagContent;break;}break;case XMLStreamConstants.START_DOCUMENT:empList = new ArrayList<>();break;}}//Print the employee list populated from XMLfor ( Employee emp : empList){System.out.println(emp);}} }class Employee{String id;String firstName;String lastName;String location;@Overridepublic String toString(){return firstName+" "+lastName+"("+id+") "+location;} }上面的輸出是:
Rakesh Mishra(111) Bangalore John Davis(112) Chennai Rajesh Sharma(113) Pune到此為止,我已經介紹了解析相同的XML文檔并執行使用所有三個解析器來填充Employee對象列表的相同任務:
- DOM解析器
- SAX解析器
- StAX解析器
翻譯自: https://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html
java sax解析xml
總結
以上是生活随笔為你收集整理的java sax解析xml_在Java中使用DOM,SAX和StAX解析器解析XML的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中州劫法场概括 中州劫法场内容是啥
- 下一篇: Spring Data MongoDB教