天天看點

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;

  @Override
  public 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 Factory
    DocumentBuilderFactory factory = 
        DocumentBuilderFactory.newInstance();

    //Get the DOM Builder
    DocumentBuilder 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;

  @Override
  public 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 XML
    for ( 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 found
      case "employee":
        emp = new Employee();
        emp.id = attributes.getValue("id");
        break;
    }
  }

  @Override
  public void endElement(String uri, String localName, 
                         String qName) throws SAXException {
   switch(qName){
     //Add the employee to list once end tag is found
     case "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;
   }
  }

  @Override
  public 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;

  @Override
  public 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 XML
    for ( Employee emp : empList){
      System.out.println(emp);
    }

  }
}

class Employee{
  String id;
  String firstName;
  String lastName;
  String location;

  @Override
  public String toString(){
    return firstName+" "+lastName+"("+id+") "+location;
  }
}
           

上面的輸出是:

Rakesh Mishra(111) Bangalore
John Davis(112) Chennai
Rajesh Sharma(113) Pune
           

到此為止,我已經介紹了解析相同的XML文檔并執行使用所有三個解析器來填充

Employee

對象清單的相同任務:

  • DOM解析器
  • SAX解析器
  • StAX解析器
參考:

來自Experiences Unlimited部落格的JCG合作夥伴 Mohamed Sanaulla 在Java中使用DOM,SAX和StAX Parser在Java中解析XML 。

翻譯自: https://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html

java sax解析xml