SAX解析xml

來源:互聯網
上載者:User

package com.xml.sax;

import java.io.File;
import java.util.Vector;

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 XMLContentHandler  extends DefaultHandler  {

    /**
     * @param args
     */
   
    private Vector<String> tagName;
     private Vector<String> tagValue;

      private int step;

     
    // 開始解析XML檔案
      public void startDocument() throws SAXException
      {
        tagName = new Vector<String>();
        tagValue = new Vector<String>();
        step = 0;
      }

      // 結束解析XML檔案
      public void endDocument() throws SAXException
      {
        for (int i = 0; i < tagName.size(); i++)
        {
          if (!tagName.get(i).equals("") || tagName.get(i) != null)
          {
            System.out.println("節點名稱:" + tagName.get(i));
            System.out.println("節點值:" + tagValue.get(i));
          }
        }
      }

      /**
        * 在解釋到一個開始元素時會調用此方法,
        * 這些重複的元素.qName是什麼? 帶首碼的屬性<name:page ll=""></name:page>這樣寫就會拋出SAXException錯誤
        * 通常情況下qName等於localName
        */
      public void startElement(String uri, String localName, String qName,
          Attributes attributes) throws SAXException
      {
        // 節點名稱
        tagName.add(qName);
        // 迴圈輸出屬性
        for (int i = 0; i < attributes.getLength(); i++)
        {
          // 擷取屬性名稱
          System.out.println("屬性名稱:" + attributes.getQName(i));
          // 擷取屬性值
          System.out.println("屬性值:"
              + attributes.getValue(attributes.getQName(i)));
        }

      }

      /**
        * 在遇到結束標籤時調用此方法
        */
      public void endElement(String uri, String localName, String qName)
          throws SAXException
      {

        step = step + 1;
      }

      /**
        * 讀取標籤裡的值,ch用來存放某行的xml的字元資料,包括標籤,初始大小是2048,
        * 每解釋到新的字元會把它添加到char[]裡。    * 注意,這個char字元會自己管理儲存的字元,
        * 並不是每一行就會重新整理一次char,start,length是由xml的元素資料確定的,
        * 暫時找不到規律,以後看原始碼.
        *   
        * 這裡一個正標籤,反標籤都會被執行一次characters,所以在反標籤時不用獲得其中的值
        */
      public void characters(char ch[], int start, int length)
          throws SAXException
      {
        // 只要當前的標籤組的長度一至,值就不賦,則反標籤不被計劃在內
        if (tagName.size() - 1 == tagValue.size())
        {
          tagValue.add(new String(ch, start, length));
        }
      }

      public static void main(String[] args)
      {
        String filename = "src/com/xml/sax/MyXml.xml";
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try
        {
          SAXParser saxParser = spf.newSAXParser();
          saxParser.parse(new File(filename), new XMLContentHandler());
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }

      public Vector getTagName()
      {
        return tagName;
      }

      public void setTagName(Vector tagName)
      {
        this.tagName = tagName;
      }

      public Vector getTagValue()
      {
        return tagValue;
      }

      public void setTagValue(Vector tagValue)
      {
        this.tagValue = tagValue;
      }

    }

 

 

 

 

 

xml檔案:

<?xml version="1.0" encoding="UTF-8"?>
<people>

  <person personid="e01" enable="true">
    <name>張三</name>
    <tel>5128</tel>
    <email>txq512@sina.com</email>
  </person>
   
  <person personid="e02" enable="false">
    <name>meixin</name>
    <tel>5252525</tel>
    <email>wnight88@sina.com</email>
  </person>
   
  <person personid="e03" enable="true">
    <name>yu</name>
    <tel>5389654</tel>
    <email>yu@188.net</email>
  </person>
   
</people>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.