• XML读取的例子


    今天需要用到用SAX对XML进行读取,在GOOGLE上看了很多例子,发现了一个写的比较好的。

    他的例子xml如下

    <?xml version="1.0"?>
    <company>
        <staff>
            <firstname>yong</firstname>
            <lastname>mook kim</lastname>
            <nickname>§</nickname>
            <salary>100000</salary>
        </staff>
    </company>

    读取的代码如下:

    package com.mkyong.test;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    public class ReadXMLUTF8FileSAX 
    {
        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 {
     
                      System.out.println(new String(ch, start, length));
     
     
                      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;
                       }
     
                    }
     
                  };
     
                  File file = new File("c:\\file.xml");
                  InputStream inputStream= new FileInputStream(file);
                  Reader reader = new InputStreamReader(inputStream,"UTF-8");
     
                  InputSource is = new InputSource(reader);
                  is.setEncoding("UTF-8");
     
                  saxParser.parse(is, handler);
     
     
                } catch (Exception e) {
                  e.printStackTrace();
                }
     
        }
    }

    两个链接:

    http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

    http://www.mkyong.com/java/how-to-read-utf-8-xml-file-in-java-sax-parser/

  • 相关阅读:
    FileWriter写数据路径问题及关闭和刷新方法的区别
    FileWriter剖析
    2018-10-27 22:44:33 c language
    2018-10-23 23:29:54 clanguage
    Just write about
    2018-10-19 00:13:35 ArrayList
    2018-10-18 22:15:32 c language
    Why do collection classes appear
    2018-10-17 22:20:39 c language
    2018-10-16 22:56:13 c language
  • 原文地址:https://www.cnblogs.com/zhidian314/p/2630030.html
Copyright © 2020-2023  润新知