Digester解析xml_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Digester解析xml

Digester解析xml

 2014/12/2 22:09:54  xuweilovejava  程序员俱乐部  我要评论(0)
  • 摘要:publicclassContact{privateStringtype;privateStringname;privateStringaddress;privateStringcity;privateStringprovince;privateStringpostalcode;privateStringcountry;//增加一个country的属性:fromprivateStringcountryFrom;privateStringtelephone;publicvoidsetType
  • 标签:XML 解析
class="java">public  class 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;

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

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

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

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

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

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

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

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

    public String getCountryFrom() {
        return countryFrom;
    }

    public void setCountryFrom(String countryFrom) {
        this.countryFrom = countryFrom;
    }
}

?

import java.io.File;
import java.io.IOException;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

public class AddressBookParser {
	 /**
     * Prints the contact information to standard output.
     *
     * @param contact the <code>Contact</code> to print out
     */
	 public void 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
     */
    public static void 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("test.xml"));
    }
}

?

<?xml version='1.0' encoding='utf-8'?>
<address-book>
    <contact myType="individual">
        <name>Zane Pasolini</name>
        <address>999 W. Prince St.</address>
        <city>New York</city>
        <province>NY</province>
        <postalcode>10013</postalcode>
        <country>USA</country>
        <telephone>1-212-345-6789</telephone>
    </contact>
    <contact myType="business">
        <name>SAMOFIX d.o.o.</name>
        <address>Ilica 47-2</address>
        <city>Zagreb</city>
        <province></province>
        <postalcode>10000</postalcode>
        <country from="cn">Croatia</country>
        <telephone>385-1-123-4567</telephone>
    </contact>
</address-book>

?文章出处:http://www.blogjava.net/alex/archive/2006/09/06/68148.html

上一篇: 设计模式杂谈 - 单例 下一篇: 没有下一篇了!
发表评论
用户名: 匿名