codestory

Analysieren einer XML-Datei mit Java SAX

  1. Die Vorstellung
  2. Das Projekt erstellen
  3. Wie funktioniert SAX ?

1. Die Vorstellung

Das ist das Bild von dem Projekt nach der Erledigung

2. Das Projekt erstellen

department-data.xml
<?xml version="1.0"?>
<department>

  <employee>
      <empNo>E01</empNo>
      <empName>KING</empName>
      <hireDate>17-11-1981</hireDate>
      <salary>100000</salary>
  </employee>

  <employee>
      <empNo>E02</empNo>
      <empName>JONES</empName>
      <hireDate>02-04-1981</hireDate>
      <salary>200000</salary>
  </employee>

</department>
MySaxHandler.java
package org.o7planning.tutorial.sax;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySaxHandler extends DefaultHandler {

   boolean foundEmpNo = false;
   boolean foundEmpName = false;
   boolean foundHireDate = false;
   boolean foundSalary = false;

   @Override
   public void startElement(String uri, String localName, String qName,
           Attributes attributes) throws SAXException {
       
       System.out.println("---------------------------");
       System.out.println("Start Element :" + qName);

       if (qName.equals("empNo")) {
           foundEmpNo = true;
       }

       if (qName.equals("empName")) {
           foundEmpName = true;
       }

       if (qName.equals("hireDate")) {
           foundHireDate = true;
       }

       if (qName.equalsIgnoreCase("salary")) {
           foundSalary = true;
       }

   }

   @Override
   public void endElement(String uri, String localName, String qName)
           throws SAXException {

       System.out.println("End Element :" + qName);
   }

   @Override
   public void characters(char ch[], int start, int length)
           throws SAXException {

       System.out.println("characters :");
       
       if (foundEmpNo) {
           System.out.println("Emp No : " + new String(ch, start, length));
           foundEmpNo = false;
       }

       if (foundEmpName) {
           System.out.println("Emp Name : " + new String(ch, start, length));
           foundEmpName = false;
       }

       if (foundHireDate) {
           System.out.println("Hire Date : " + new String(ch, start, length));
           foundHireDate = false;
       }

       if (foundSalary) {
           System.out.println("Salary : " + new String(ch, start, length));
           foundSalary = false;
       }

   }

}
MainReadXML.java
package org.o7planning.tutorial.sax;

import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;

public class MainReadXML {

   public static void main(String[] args) {

       try {
           SAXParserFactory factory = SAXParserFactory.newInstance();
           SAXParser saxParser = factory.newSAXParser();

           DefaultHandler handler = new MySaxHandler();

           File file = new File("department-data.xml");
           saxParser.parse(file, handler);

       } catch (Exception e) {
           e.printStackTrace();
       }

   }

}
Und das ist das Ergebnis von dem Starten der Klasse MainReadXML
---------------------------
Start Element :department
characters :
---------------------------
Start Element :employee
characters :
---------------------------
Start Element :empNo
characters :
Emp No : E01
End Element :empNo
characters :
---------------------------
Start Element :empName
characters :
Emp Name : KING
End Element :empName
characters :
---------------------------
Start Element :hireDate
characters :
Hire Date : 17-11-1981
End Element :hireDate
characters :
---------------------------
Start Element :salary
characters :
Salary : 100000
End Element :salary
characters :
End Element :employee
characters :
---------------------------
Start Element :employee
characters :
---------------------------
Start Element :empNo
characters :
Emp No : E02
End Element :empNo
characters :
---------------------------
Start Element :empName
characters :
Emp Name : JONES
End Element :empName
characters :
---------------------------
Start Element :hireDate
characters :
Hire Date : 02-04-1981
End Element :hireDate
characters :
---------------------------
Start Element :salary
characters :
Salary : 200000
End Element :salary
characters :
End Element :employee
characters :
End Element :department

3. Wie funktioniert SAX ?

Jetzt erklären wir die Arbeitsweise von SAX nach dem oben erwähnten Beispiel.