現在的位置: 首頁 > 綜合 > 正文

使用Digester解析xml

2014年01月11日 ⁄ 綜合 ⁄ 共 4274字 ⁄ 字號 評論關閉

addressbook.xml

<?xml version='1.0' encoding='utf-8'?>
<address-book>
    <contact type="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 type="business">
        <name>SAMOFIX d.o.o.</name>
        <address>Ilica 47-2</address>
        <city>Zagreb</city>
        <province></province>
        <postalcode>10000</postalcode>
        <country>Croatia</country>
        <telephone>+385 1 123 4567</telephone>
    </contact>
    <contact type="populater">
        <name>QAMOFIX d.o.o.</name>
        <address>Ilica 87-2</address>
        <city>Zagreb</city>
        <province></province>
        <postalcode>10200</postalcode>
        <country>Croatia</country>
        <telephone>+7885 1 213 487</telephone>
    </contact>
    <contact type="populater">
        <name>QAMOFIX d.o.o.</name>
        <address>Ilica 87-2</address>
        <city>Zagreb</city>
        <province></province>
        <postalcode>10200</postalcode>
        <country>Croatia</country>
        <telephone>+7885 1 213 487</telephone>
    </contact>
</address-book>

DigesterXMLHandler.java

package com.lucene;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class DigesterXMLHandler {
	
	
	public DigesterXMLHandler() {
		
		Digester digester=new Digester();
		digester.setValidating(false);
		
		Contact contact=new Contact();
		
		digester.push(contact);
		
		digester.addObjectCreate("address-book/contact", Contact.class);
		
		digester.addSetProperties("address-book/contact", "type", "type");
		
		digester.addBeanPropertySetter("address-book/contact/name", "name");
		digester.addBeanPropertySetter("address-book/contact/address", "address");
		digester.addBeanPropertySetter("address-book/contact/city", "city");
		digester.addBeanPropertySetter("address-book/contact/province", "province");
		digester.addBeanPropertySetter("address-book/contact/postalcode", "postalcode");
		digester.addBeanPropertySetter("address-book/contact/country", "country");
		digester.addBeanPropertySetter("address-book/contact/telephone", "telephone");
		
		
		digester.addSetNext("address-book/contact", "addContact");
		
		
		try {
			Contact object = (Contact) digester.parse(DigesterXMLHandler.class.getClassLoader().getResourceAsStream("addressbook.xml"));
			
			List<Contact> list=object.getList();
			for (Contact con : list) {
				System.out.println(
						con.getType()+"\t"+con.getName()+"\t"+
						con.getAddress()+"\t"+con.getCity()+"\t"+
						con.getProvince()+"\t"+con.getPostalcode()+
						"\t"+con.getCountry()+"\t"+con.getTelephone()
				);
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new DigesterXMLHandler();
	}
	
	
	public static class Contact {
		
		public List<Contact> list=new ArrayList<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 List<Contact> addContact(Contact contact){
	    	this.list.add(contact);
	    	return list;
	    }
	    
	    
	    
	    
	    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 List<Contact> getList() {
			return list;
		}

		public void setList(List<Contact> list) {
			this.list = list;
		}




		@Override
		public String toString() {
			return "Contact [type=" + type + ", name="
					+ name + ", address=" + address + ", city=" + city
					+ ", province=" + province + ", postalcode=" + postalcode
					+ ", country=" + country + ", telephone=" + telephone + "]";
		}

		
	    
	  }

	
}

運行結果:

individual    Zane Pasolini    999 W. Prince St.    New York    NY    10013    USA    +1 212 345 6789
business    SAMOFIX d.o.o.    Ilica 47-2    Zagreb        10000    Croatia    +385 1 123 4567
populater    QAMOFIX d.o.o.    Ilica 87-2    Zagreb        10200    Croatia    +7885 1 213 487
populater    QAMOFIX d.o.o.    Ilica 87-2    Zagreb        10200    Croatia    +7885 1 213 487

抱歉!評論已關閉.