这个 Java 程序是如何运行的? [英] How does this Java Program Run?

查看:23
本文介绍了这个 Java 程序是如何运行的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 Java 中的 DOMParserSAXParser.我对 DOMParser 毫无疑问,人们更喜欢 SAXParser 而不是 DOMParser,因为它需要内存.但是我理解 SAXParser 的概念,我无法在此代码下:

I read about DOMParser and SAXParser in Java. I have no doubts in DOMParser and people prefer SAXParser than DOMParser, because of the memory it takes. However I understand the concept of SAXParser, i could not able to under this code:

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 args[]) {

  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("/home/anto/Groovy/Java/file.xml", handler);

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

}

.xml 文件是:

<?xml version="1.0"?>
<company>
    <staff>
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff>
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

当我运行程序时,我得到如下输出:

And when i run the program i get the output like this:

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

输出看起来很好,但我对输出感到困惑!输出的顺序是如何打印的?哪个处理这个?由于这是我第一次阅读 SAX 和 DOM,我无法理解,请帮助我.

The output looks very fine, but i'm confused with the output! How the order of the output is been printed? Which handles this? Since this is the first time I have read SAX And DOM, i could not able to figure it, kindly help me.

推荐答案

SAX 是基于事件的.因此,每次它看到开始标记、属性、标记内的字符、结束标记等时,它都会调用处理程序的适当函数.

SAX is event-based. So, each time it sees a start tag, attribute, characters within a tag, end tag, ... it calls the appropriate function of the handler.

所以这里的流程是:

  1. 查看company标签,调用startElement
  2. 查看staff标签,调用startElement
  3. 查看 firstname 标签,为它调用 startElement(设置一个布尔值)
  4. 查看字符(yong"),为它们调用 characters 函数(查看设置了哪个布尔值并打印相应的消息并清除标志)
  5. 看到结束的firstname标签,调用endElement函数
  1. See the company tag, call startElement for it
  2. See the staff tag, call startElement for it
  3. See the firstname tag, call startElement for it (which sets a boolean)
  4. See characters ("yong"), call the characters function for them (which sees which boolean is set and prints the appropriate message and clears the flag)
  5. See the closing firstname tag, call the endElement function

...

这篇关于这个 Java 程序是如何运行的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆