Java解析XML之 Digester的一个小例子

阅读更多

XML文档

xml version="1.0" encoding="UTF-8" ?>
DOCTYPE po SYSTEM "src/sample.dtd" >
< po  id ="po121"  submitted ="2001-10-05"
    xmlns
="http://www.csdn.net" >
    
< billTo  id ="addr-1" >
        
< company > The Redzero Cop. company >
        
< street > Jianguo Road 1ST street >
        
< street > Building 1 street >
        
< city > Haidian city >
        
< state > Beijing state >
        
< postalCode > 100071 postalCode >
    
billTo >
    
< shipTo  href ="addr-1" />
    
< order >
        
< item  sku ="o318-BP"  quantity ="5" >
            
< description > J2EE programmic description >
        
item >
        
< item  sku ="o947-TI"  quantity ="12" >
            
< description > Java WEB Service description >
        
item >
        
< item  sku ="o008-PR"  quantity ="133" >
            
< description > C++ develop description >
        
item >
    
order >
po >

 Java对象树定义

// 根节点,表示一个订单Po
public   class  Po
... {
    
private String id;
    
private String submitted;
    
private BillTo billTo;
    
private ShipTo shipTo;
    
private Order order;
    
public BillTo getBillTo()
    
...{
        
return billTo;
    }

    
public void setBillTo(BillTo billTo)
    
...{
        
this.billTo = billTo;
    }

    
public Order getOrder()
    
...{
        
return order;
    }

    
public void setOrder(Order order)
    
...{
        
this.order = order;
    }

    
public ShipTo getShipTo()
    
...{
        
return shipTo;
    }

    
public void setShipTo(ShipTo shipTo)
    
...{
        
this.shipTo = shipTo;
    }

    
public String getId()
    
...{
        
return id;
    }

    
public void setId(String id)
    
...{
        
this.id = id;
    }

    
public String getSubmitted()
    
...{
        
return submitted;
    }

    
public void setSubmitted(String submitted)
    
...{
        
this.submitted = submitted;
    }
    
}

// Po的子节点,标识送货地址,对应
public   class  BillTo  extends  Address
... {
    
private String id;

    
public String getId()
    
...{
        
return id;
    }


    
public void setId(String id)
    
...{
        
this.id = id;
    }
    
}


public   class  ShipTo  extends  Address
... {
    
private String href;

    
public String getHref()
    
...{
        
return href;
    }


    
public void setHref(String href)
    
...{
        
this.href = href;
    }

    
}

public   class  Address
... {
    
private String company;
    
private String city;
    
private String state;
    
private String postalCode;
    
private ArrayList streets  = new ArrayList();
    
    
public void addStreet(String street)...{
        
this.streets.add(street);
    }

    
public String getCity()
    
...{
        
return city;
    }

    
public void setCity(String city)
    
...{
        
this.city = city;
    }

    
public String getCompany()
    
...{
        
return company;
    }

    
public void setCompany(String company)
    
...{
        
this.company = company;
    }

    
public String getPostalCode()
    
...{
        
return postalCode;
    }

    
public void setPostalCode(String postalCode)
    
...{
        
this.postalCode = postalCode;
    }

    
public String getState()
    
...{
        
return state;
    }

    
public void setState(String state)
    
...{
        
this.state = state;
    }

你可能感兴趣的:(XML,Java,Web,.net,C)