<title>Java and XML</title> 
    
<!-- Chapter List -->
    
<contents>
        
<chapter title="Introduction" number="1">
            
<topic name="XML Matters"/> 
            
<topic name="What's Important"/>
            
<topic name="The Essentials"/>
            
<topic name="What&apos;s Next?"/>
        
</chapter>
        
<chapter title="Nuts and Bolts" number="2">
            
<topic name="The Basics"/>
            
<topic name="Constraints"/>
            
<topic name="Transformations"/>
            
<topic name="And MoreSax读取xml文件(二)"/>
            
<topic name="What&apos;s Next?"/>
        
</chapter>
        
<chapter title="SAX" number="3">
            
<topic name="Getting Prepared"/>
            
<topic name="SAX Readers"/>
            
<topic name="Content Handlers"/>
            
<topic name="Gotcha!"/>
            
<topic name="What&apos;s Next?"/>
        
</chapter>       
    
</contents>
</book>

java文件

 DefaultHandler {

    
private int index1 = 0;
    
private int index2 = 0;

    
public void characters(char[] ch, int start, int length)
            
throws SAXException {

        
try {
            String str 
= new String(ch, start, length);
            
if (str.trim().length() > 0) {
                System.out.println(
"本书<<" + str + ">>的目录");
            }

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

    
public void endDocument() throws SAXException {
        System.out.println(
"解析结束:");
    }

    
public void endElement(String uri, String localName, String qName)
            
throws SAXException {

        
if (qName.equalsIgnoreCase("chapter"))
            
this.index2 = 0;
    }

    
public void startDocument() throws SAXException {
        System.out.println(
"解析开始: ");
    }

    
public void startElement(String uri, String localName, String qName,
            Attributes atts) 
throws SAXException {

        
if (qName.equalsIgnoreCase("chapter")) {
            index1
++;
            
for (int i = 0; i < atts.getLength(); i++) {
                String attName 
= atts.getQName(i);
                
if (attName.equalsIgnoreCase("title")) {
                    System.out.println(
"" + index1 + "章:" + atts.getValue(i));
                }
            }
        }

        
if (qName.equalsIgnoreCase("topic")) {
            index2
++;
            
for (int i = 0; i < atts.getLength(); i++) {
                String attName 
= atts.getQName(i);
                
if (attName.equalsIgnoreCase("name")) {
                    System.out.println(
"     第" + index2 + "部分:"
                            
+ atts.getValue(i));
                }
            }
        }
    }

    
public static void main(String[] args) throws Exception {
        String xmlURI 
= "contents.xml";  //相对路径
        XMLReader reader 
= XMLReaderFactory.createXMLReader();
        reader.setContentHandler(
new SaxXML());
        InputSource inputSource 
= new InputSource(xmlURI);
        reader.parse(inputSource);

        
/*   第二种读取方法
                    SAXParserFactory factory=SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(true);
            SAXParser parser=factory.newSAXParser();
            parser.parse("contents.xml", new SaxXML());
*/
    }
}

 

>>结果:

解析开始:
本书<<Java and XML>>的目录
第1章:Introduction
     第1部分:XML Matters
     第2部分:What's Important
     第3部分:The Essentials
     第4部分:What's Next?
第2章:Nuts and Bolts
     第1部分:The Basics
     第2部分:Constraints
     第4部分:Transformations
     第5部分:And More...
     第6部分:What's Next?
第3章:SAX
     第1部分:Getting Prepared
     第2部分:SAX Readers
     第3部分:Content Handlers
     第4部分:Gotcha!
     第5部分:What's Next?
解析结束:

相关文章:

  • 2021-10-13
  • 2021-06-21
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2021-10-04
猜你喜欢
  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
相关资源
相似解决方案