1. 程式人生 > >用Digester解析xml到bean

用Digester解析xml到bean

publicclass AddressBookParser
{
    
/**
     * Prints the contact information to standard output.
     *
     * 
@param contact the <code>Contact</code> to print out
     
*/publicvoid addContact(Contact contact)
    {
        System.out.println(
"TYPE: "+ contact.getType());
        System.out.println(
"NAME: "+ contact.getName());
        System.out.println(
"    ADDRESS:    "+ contact.getAddress());
        System.out.println(
"    CITY:       "+ contact.getCity());
        System.out.println(
"    PROVINCE:   "+ contact.getProvince());
        System.out.println(
"    POSTALCODE: "+ contact.getPostalcode());
        System.out.println(
"    COUNTRY:    "+ contact.getCountry());
        System.out.println(
"    COUNTRY-From:    "+ contact.getCountryFrom());
        System.out.println(
"    TELEPHONE:  "+ contact.getTelephone());
    }

    
/**
     * Configures Digester rules and actions, parses the XML file specified
     * as the first argument.
     *
     * 
@param args command line arguments
     
*/publicstaticvoid main(String[] args) throws IOException, SAXException
    {
        
// instantiate Digester and disable XML validation        Digester digester =new Digester();
        digester.setValidating(
false);

        
// instantiate AddressBookParser class        digester.addObjectCreate("address-book", AddressBookParser.class );
        
// instantiate Contact class        digester.addObjectCreate("address-book/contact", Contact.class );

        
// set type property of Contact instance when 'type' attribute is found
        
//對有屬性的值通過setProperties方法
        digester.addSetProperties(
"address-book/contact",         "myType""type");

        
// set different properties of Contact instance using specified methods
        
//addCallMethod與addBeanPropertySetter等價
        
// 引數 0代表一個引數,預設就是當前讀的資料
        digester.addCallMethod(
"address-book/contact/name",       "setName"0);
        digester.addCallMethod(
"address-book/contact/address",    "setAddress"0);
        digester.addCallMethod(
"address-book/contact/address",    "setAddress",0);
        digester.addCallMethod(
"address-book/contact/city",       "setCity"0);
        digester.addCallMethod(
"address-book/contact/province",   "setProvince"0);
        digester.addCallMethod(
"address-book/contact/postalcode""setPostalcode"0);
        digester.addCallMethod(
"address-book/contact/country",    "setCountry"0);



        
//增加country的屬性 : from        digester.addSetProperties("address-book/contact/country","from","countryFrom");
        digester.addCallMethod(
"address-book/contact/telephone",  "setTelephone"0);

        
// call 'addContact' method when the next 'address-book/contact' pattern is seen        digester.addSetNext("address-book/contact",               "addContact" );

        
// now that rules and actions are configured, start the parsing process        AddressBookParser abp = (AddressBookParser) digester.parse(new File("c://addressbook.xml"));
    }

    
/**
     * JavaBean class that holds properties of each Contact entry.
     * It is important that this class be public and static, in order for
     * Digester to be able to instantiate it.
     
*/publicstaticclass Contact
    {
        
private String type;
        
private String name;
        
private String address;
        
private String city;
        
private String province;
        
private String postalcode;
        
private String country;
        //增加一個country的屬性: from
private String countryFrom;private String telephone;

        
publicvoid setType(String newType)
        {
            type 
= newType;
        }
        
public String getType()
        {
            
return type;
        }

        
publicvoid setName(String newName)
        {
            name 
= newName;
        }
        
public String getName()
        {
            
return name;
        }

        
publicvoid setAddress(String newAddress)
        {
            address 
= newAddress;
        }
        
public String getAddress()
        {
            
return address;
        }

        
publicvoid setCity(String newCity)
        {
            city 
= newCity;
        }
        
public String getCity()
        {
            
return city;
        }

        
publicvoid setProvince(String newProvince)
        {
            province 
= newProvince;
        }
        
public String getProvince()
        {
            
return province;
        }

        
publicvoid setPostalcode(String newPostalcode)
        {
            postalcode 
= newPostalcode;
        }
        
public String getPostalcode()
        {
            
return postalcode;
        }

        
publicvoid setCountry(String newCountry)
        {
            country 
= newCountry;
        }
        
public String getCountry()
        {
            
return country;
        }

        
publicvoid setTelephone(String newTelephone)
        {
            telephone 
= newTelephone;
        }
        
public String getTelephone()
        {
            
return telephone;
        }

        
public String getCountryFrom() {
            
return countryFrom;
        }

        
publicvoid setCountryFrom(String countryFrom) {
            
this.countryFrom = countryFrom;
        }
    }
}