同情lz,ms现在很少人用sax解析xml,过时了,
用dom4j超简单的,几句就可以,性能也是众所周之的,
lz就不要重新造轮子

解决方案 »

  1.   

    谢谢。。总算有个回答的了。。我开始就用的dom4j但是太占内存了!所以才考虑sax的。。有没有好的办法解决占用内存大的问题呀?!
      

  2.   

    我帮LZ顶一个,也想学习下解析大的XML。以前用dom4j没有遇到过大的文件。。
      

  3.   

    以前用过sax,我大概是这么用的:
    <?xml version="1.0" encoding="UTF-8"?>
    <setting>
    <nameset>
        <book>sax解析xml中出现问题效率比较低及复件 (36) 中国传统文化.ppt</book>
    </nameset>
    </setting>package mytest;import org.xml.sax.Attributes;
    import org.xml.sax.ContentHandler;
    import org.xml.sax.Locator;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;public class SaxTest { /**
     * @param args
     * @throws SAXException 
     */
    public static void main(String[] args) throws SAXException {
    // TODO Auto-generated method stub

            XMLReader reader = XMLReaderFactory.createXMLReader() ;
            ContentHandler contentHandler = new MyContentHandler();
            reader.setContentHandler( contentHandler );
            try{
                reader.parse("setting.xml"); 
            }catch(Exception e){
                e.printStackTrace(); 
            } 

    }}class MyContentHandler implements ContentHandler {
    private StringBuffer buf; public void setDocumentLocator(Locator locator) {
    } public void startDocument() throws SAXException {
    buf = new StringBuffer();
    } public void endDocument() throws SAXException {
    } public void processingInstruction(String target, String instruction)
    throws SAXException {
    } public void startPrefixMapping(String prefix, String uri) {
    } public void endPrefixMapping(String prefix) {
    } public void startElement(String namespaceURI, String localName,
    String fullName, Attributes attributes) throws SAXException {
    } public void endElement(String namespaceURI, String localName,
    String fullName) throws SAXException {
    String nullStr = "";
    if (!buf.toString().trim().equals(nullStr)) {
    if (fullName.trim().equals("book")) {
    System.out.println(buf.toString().trim());
    }
    }
    buf.setLength(0);
    } public void characters(char[] chars, int start, int length)
    throws SAXException {
    buf.append(chars, start, length);
    } public void ignorableWhitespace(char[] chars, int start, int length)
    throws SAXException {
    } public void skippedEntity(String name) throws SAXException {
    }
    }