100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java sax解析xml_在Java中使用DOM SAX和StAX解析器解析XML

java sax解析xml_在Java中使用DOM SAX和StAX解析器解析XML

时间:2021-01-10 06:07:23

相关推荐

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)BangaloreJohn Davis(112)ChennaiRajesh 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)BangaloreJohn Davis(112)ChennaiRajesh 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) BangaloreJohn Davis(112) ChennaiRajesh Sharma(113) Pune

到此为止,我已经介绍了解析相同的XML文档并执行使用所有三个解析器来填充Employee对象列表的相同任务:

DOM解析器 SAX解析器 StAX解析器 参考:来自Experiences Unlimited博客的JCG合作伙伴 Mohamed Sanaulla 在Java中使用DOM,SAX和StAX Parser在Java中解析XML 。

翻译自: //05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html

java sax解析xml

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。