| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 522 人关注过本帖
标题:非常不错读取 XML 的方式
只看楼主 加入收藏
xioun
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:68
专家分:309
注 册:2013-4-8
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:2 
非常不错读取 XML 的方式
package com.mkyong.test;
 
import
import
import
import
import
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();
            }
 
    }
}
搜索更多相关主题的帖子: package public import 
2014-06-20 09:36
xioun
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:68
专家分:309
注 册:2013-4-8
收藏
得分:0 
<?xml version="1.0"?>
<company>
    <staff>
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>§</nickname>
        <salary>100000</salary>
    </staff>
</company>

命若天定,我就破了这个天!
2014-06-20 09:37
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:20 
SAX固然是好,但实际开发多数用的是dom4j

思考赐予新生,时间在于定义
2014-06-20 12:17
快速回复:非常不错读取 XML 的方式
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019769 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved