lucene-使用Digester分析XML索引

1、隶属于Jakarta Commons项目的一个子项目提供了一个简单的上层接口来将XML类型的文档映射为JAVA对象。

2、DigesterXMLHandler类用来解析XML格式的文档。

public class DigesterXMLHandler implements DocumentHandler{

   private Digester dig;

   private static Documentdoc;

   publicDigesterXMLHandler(){

     dig=new Digester();

     dig.setValidating(false);

      //首先创建DigesterXMLHandler实例

     dig.addObjectCreate("address-book",DigesterXMLHandler.class);

     //找到contact子元素,创建一个contact类实例

     dig.addObjectCreate("address-book/contact",Contact.class);

      //当Digester找到<contact>参数的type属性时我们就需要设置Contact实例的type属性。

     dig.adSetProperties("address-book","type","type");

     //设置几个规则,用来设置contact属性。

      dig.addCallMethod("address-book/contact/name","setName",0);

      dig.addCallMethod("address-book/contact/address","setAddress",0);

      dig.addCallMethod("address-book/contact/city","setCity",0);

      dig.addCallMethod("address-book/contact/province","setProvince",0);

      dig.addCallMethod("address-book/contact/postalcode","setPostalcode",0);

      dig.addCallMethod("address-book/contact/country","setCountry",0);

      dig.addCallMethod("address-book/contact/telephone","setTelephone",0);

     dig.addSetNext("address-book/contact","populateDocument");

   }

   public sychronized DocumentgetDocument(InputStream is) throws DocumentHandlerException{

      try{

          dig.parse(is);//开始解析XML格式的InputStream 输入流
      }

      catch (IOException e){

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

       }

      catch (SAXExceptoin e){

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

      }

      return doc;

   }

  

  public voidpopulateDocument(Contact contact){

   //将已经取出的各Field组装到Lucene的Document对象里

    doc=new Document();

     doc.add(Field.Keyword("type",contact.getType()));

    doc.add(Field.Keyword("name",contact.getName()));

    doc.add(Field.Keyword("address",contact.getAddress()));

    doc.add(Field.Keyword("city",contact.getCity()));

    doc.add(Field.Keyword("province",contact.getProvince()));

    doc.add(Field.Keyword("postalcode",contact.getPostalcode()));

    doc.add(Field.Keyword("country",contact.getCountry()));

    doc.add(Field.Keyword("telephone",contact.getTelephone()));

  }

  //重载了每个contact实例入口的JAVABEAN类

  public static class Contact{

    private String type;

    private String name;

    private String address;

    private String city;

    private String province;

    private String postalcode;

    private String country;

    private String telephone;

    public void setType(String newType){

        type=newType();

    }

    public String getType(){

        return type;

    }

    public String setName(String newName){

        name=newName;

    }

    public String getName(){

         returnname;

    }

    ..........//依次设置city、province、postalcode、country、telephone

    ..........

   }

   public static voidmain(String[] args) throws Exception{

        DigesterXMLHandler handler=new DigesterXMLHandler();

        Document doc=

           handler.getDocument(new FileInputStream(new File(args[0]));

         System.out.println(doc);

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值