t.me/atinfo_chat Telegram группа по автоматизации тестирования

Как считать XML файл с помощью DOM и SAX парсера?


(Mykhailo Poliarush) #1

DOM парсер медленный и потребляет много памяти, если парсить большой XML файл. В этом случае, используйте SAX парсер, так как он быстрее и использует меньше памяти. 

Входной XML:

```xml yong mook kim mkyong 100000 low yin fong fong fong 200000 ```

DOM parser

```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File;

public class ReadXMLFile {
 
 public static void main(String argv[]) {
 
 try {
 
    File fXmlFile = new File("c:\\file.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
 
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("staff");
    System.out.println("-----------------------");
 
    for (int temp = 0; temp < nList.getLength(); temp++) {
 
       Node nNode = nList.item(temp);	    
       if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
          Element eElement = (Element) nNode;
 
          System.out.println("First Name : "  + getTagValue("firstname",eElement));
          System.out.println("Last Name : "  + getTagValue("lastname",eElement));
          System.out.println("Nick Name : "  + getTagValue("nickname",eElement));
          System.out.println("Salary : "  + getTagValue("salary",eElement));
 
        }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
 
 private static String getTagValue(String sTag, Element eElement){
    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0); 
 
    return nValue.getNodeValue();    
 }
 
}
</p><p>Результат:&nbsp;</p><pre>
Root element :company
-----------------------
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000</pre><h2>SAX parser</h2><p>
````java
    package com.mkyong.test;
     
    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 ReadXMLFileSAX {
     
     public static void main(String argv[]) {
     
      try {
     
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
     
         DefaultHandler handler = new DefaultHandler() {
     
         boolean bfname = false;
         boolean blname = false;
         boolean bnname = false;
         boolean bsalary = false;
     
         public void startElement(String uri, String localName,
            String qName, Attributes attributes)
            throws SAXException {
     
            System.out.println("Start Element :" + qName);
     
            if (qName.equalsIgnoreCase("FIRSTNAME")) {
               bfname = true;
            }
     
            if (qName.equalsIgnoreCase("LASTNAME")) {
               blname = true;
            }
     
            if (qName.equalsIgnoreCase("NICKNAME")) {
               bnname = true;
            }
     
            if (qName.equalsIgnoreCase("SALARY")) {
               bsalary = true;
            }
     
         }
     
         public void endElement(String uri, String localName,
              String qName)
              throws SAXException {
     
              System.out.println("End Element :" + qName);
     
         }
     
         public void characters(char ch[], int start, int length)
             throws SAXException {
     
             if (bfname) {
                System.out.println("First Name : "
                    + new String(ch, start, length));
                bfname = false;
              }
     
              if (blname) {
                  System.out.println("Last Name : "
                      + new String(ch, start, length));
                  blname = false;
               }
     
              if (bnname) {
                  System.out.println("Nick Name : "
                      + new String(ch, start, length));
                  bnname = false;
               }
     
              if (bsalary) {
                  System.out.println("Salary : "
                      + new String(ch, start, length));
                  bsalary = false;
               }
     
            }
     
          };
     
          saxParser.parse("c:\\file.xml", handler);
     
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
     
    }

Результат:

Start Element :company
Start Element :staff
Start Element :firstname
First Name : yong
End Element :firstname
Start Element :lastname
Last Name : mook kim
End Element :lastname
Start Element :nickname
Nick Name : mkyong
End Element :nickname
Start Element :salary
Salary : 100000
End Element :salary
End Element :staff
Start Element :staff
Start Element :firstname
First Name : low
End Element :firstname
Start Element :lastname
Last Name : yin fong
End Element :lastname
Start Element :nickname
Nick Name : fong fong
End Element :nickname
Start Element :salary
Salary : 200000
End Element :salary
End Element :staff
End Element :company

(Hohner) #2

позвольте добавить ссылку на статью (да и вообще полезный ресурс) http://www.vogella.com/articles/JavaXML/article.html#javaxpath