• 使用SAX解析XML实例根据属性查找


    XML:

    目的:输入sn=01 把相应的student的肮name和age取回

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="student.xsl"?>
    <students>
      
    <student sn="01">
        
    <name>gao</name>
        
    <age>27</age>
      
    </student>
      
    <student sn="02">
        
    <name>yu</name>
        
    <age>24</age>
      
    </student>
    </students>
    package sax;

    import java.io.File;
    import java.io.IOException;
    import java.util.Stack;

    import javax.xml.parsers.ParserConfigurationException;
    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 StudentLookup extends DefaultHandler {
        
    private String name;
        
    private String age;
        
        
    private String attrName;
        
    private String attrValue;
        
        
    private Stack tagsStack=new Stack();
        
        
    public void setAttrName(String attrName)
        
    {
            
    this.attrName = attrName;
        }


        
    public void setAttrValue(String attrValue)
        
    {
            
    this.attrValue = attrValue;
        }


        
    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException
        
    {
            
            
    if(tagsStack.empty())
            
    {
                
                
    if("student".equals(qName))
                
    {
                    
                    
    int len=attrs.getLength();
                    
    for(int i=0;i<len;i++)
                    
    {
                        
                        
    if(attrName.equals(attrs.getQName(i)) && 
                                attrValue.equals(attrs.getValue(i)))
                        
    {
                            tagsStack.push(qName);
                            
    break;
                        }

                    }

                }

            }

            
    else
            
    {
                tagsStack.push(qName);
            }

        }

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

            
    if(!tagsStack.empty())
            
    {
                String tag
    =(String) tagsStack.peek();
                
    if("name".equals(tag))
                    name
    =new String(ch,start,length);
                
    if("age".equals(tag))
                    age
    =new String(ch,start,length);
            }

        }


        
    public void endElement(String uri, String localName, String qName) throws SAXException
        
    {
            
    if(!tagsStack.empty())
            
    {
                String tag
    =(String)tagsStack.pop();
                
    if(tagsStack.empty() && "student".equals(tag))
                
    {
                    System.out.println(
    "name: "+name);
                    System.out.println(
    "age: "+age);
                    
    throw new SAXException("找到了匹配的学生");
                }

            }

        }


        
    public void endDocument() throws SAXException
        
    {
            System.out.println(
    "没有找到匹配的学生");
        }


        
    public String getName() {
            
    return name;
        }


        
    public void setName(String name) {
            
    this.name = name;
        }


        
    public String getAge() {
            
    return age;
        }


        
    public void setAge(String age) {
            
    this.age = age;
        }


        
    public Stack getTagsStack() {
            
    return tagsStack;
        }


        
    public void setTagsStack(Stack tagsStack) {
            
    this.tagsStack = tagsStack;
        }


        
    public String getAttrName() {
            
    return attrName;
        }


        
    public String getAttrValue() {
            
    return attrValue;
        }


        
    public static void main(String[] args) {
             
    if(args.length!=1){
                 System.out.println(
    "usage:java StudentLookup key=value");
                 System.exit(
    0);
             }

             
    int index=args[0].indexOf("=");
             
    if(index==-1){
                 System.out.println(
    "usage:java StudentLookup key=value");
                 System.exit(
    0);
             }

             String str
    =args[0].substring(0,index);
             StudentLookup sl
    =new StudentLookup();
             sl.setAttrName(str);
             
             String str1
    =args[0].substring(index+1);
             sl.setAttrValue(str1);
             
            
             String realpath
    =System.getProperty("user.dir")+File.separator+"src"+File.separator+"sax"+File.separator+"student.xml";    
               SAXParserFactory spf
    =SAXParserFactory.newInstance();
               
    try {
                SAXParser parser
    =spf.newSAXParser();
                parser.parse(
    new File(realpath), sl);
            }
     catch (ParserConfigurationException e) {
                e.printStackTrace();
            }
     catch (SAXException e) {
                e.printStackTrace();
            }
     catch (IOException e) {

                e.printStackTrace();
            }


        }




    }

    用异常完成查找,结果如下:

    name: gao
    age: 27
    org.xml.sax.SAXException: 找到了匹配的学生
     at sax.StudentLookup.endElement(StudentLookup.java:84)
     at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
     at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
     at javax.xml.parsers.SAXParser.parse(SAXParser.java:375)
     at javax.xml.parsers.SAXParser.parse(SAXParser.java:311)
     at sax.StudentLookup.main(StudentLookup.java:148)

    如果不想打印出异常堆栈,则只要屏蔽掉
    catch (SAXException e) {
       e.printStackTrace();
    }

  • 相关阅读:
    Mac Office2016 安装及破解
    Axure 下载安装
    MacBook Pro常用快捷键
    MySQL 关于case when结合group by用时的写法举例
    RFID第二次作业
    Javascript函数柯里化(curry)
    binarySearch二分查找——Javascript实现
    KMP算法——Javascript实现
    从输入 URL 到页面加载完成的过程中都发生了什么事情?
    丁又专老师作业——Java检测代码
  • 原文地址:https://www.cnblogs.com/duanxz/p/2628445.html
Copyright © 2020-2023  润新知