lucene-用SAX API进行XML解析并索引

1、对XML进行解析使用XML(SAX)的API定义了一个事件驱动的接口。在这个接口中,当某个分析事件发生时,解析器会调用几个方法中的一个响应,而这些方法是由调用程序提供的。这些触发事件包括文档或文档的开始、结束或解析出错。

2、我们使用Xerces2 Java Parser解析器。

3、

XML文档片段

<?xml version='1.0'encoding='utf-8'?>

<adddress-book>

   <contact type="individual">

      <name>ZanePasolini</name>

      <address>999 W.PrinceSt.</address>

       <city>NewYork</city>

      <province>NY</province>

      <postalcode>10013</postalcode>

      <country>USA</country>

      <telephone>+1 212 3456789</telephone>

   </contact>

</address-book>

4、

public class SAXXMLHandler extends DefaultHandler implementsDocument{

    privateStringBuffer elementBuffer=new StringBuffer();

    privateHashMap attributeMap;

   

    privateDocument doc;

  

    publicDocument getDocument(InputStream is) throwsDocumentHandlerException{//启动解析器

        SAXParseFactory spf=SAXParserFactory.newInstance();

         try{

             SAXParserparser=spf.newSAXParser();      

             parser.parse(is,this);

        }

        catch (IOException e){

            throw new DocumentHandlerException("cannot parse xmldocument",e);
        }

        catch (ParserConfigurationException e){

            throw new DocumentHandlerException("cannot parse xmldocument",e);

        }

        catch (SAXException e){

            throw new DocumentHandlerException("cannot parse xmldocument",e);

        }

       return doc;

    }

    publicvoid startDocument(){//解析开始事件触发这个方法

       doc=new Document();

    }

    publicvoid startElement(String uri,String localName,StringqName,Attributes atts)

    throws SAXException{//每当解析器找到一个新的XML元素的起始标志,都会调用startElement方法

        elementBuffer.setLength();//清空

         attributeMap.clear();//清空上一个元素的相关记录

        if (atts.getLength()>0){

           attributeMap=new HashMap();

           for (int i=0;i<atts.getLength();i++){

               attributeMap.put(atts.getQName(i),atts.getValue(i));

            }

        }

    }

    publicvoid characters (){在处理独立的XML元素过程,characters可能被多次调用到,找到cdata

      elementBuffer.append(text,start,length);

    }

    publicvoid endElement(Strig uri,String localNmae,String qName){

   //当XML元素关闭时调用

    if(qName.equals("address-book")){

       return;

    }

   else if(qName.equals("contact")){  

     Iterator iter=attributemap.keySet().iterator();

     while (iter.hasNext(){

        String attName=(String) iter.next();

        String attValue=(String) attributeMap.get(attName);

        doc.add(Field.keyword(attName,attvalue));

     }

   }

   else{

    doc.add(Field.keyword(qName,elementBuffer.toString()));

   }

 }  

  public static void main(Stringargs[]) throws Exception{

    SAXXMLHandler handler=new SAXXMLHandler();

    Document doc=handler.getDocument(new FileInputStream(newFile(args[0])));

    System.out.println(doc);

  }  

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值