Java Digester解析

时间:2013-05-04 05:52:21

标签: java xml apache-commons-digester

我正在对xml进行java对象解析,但解析的时间是 billingDetails expiryDetails * null * 我的xml是_

<?xml version="1.0" encoding="UTF-8" standalone="yes">
<item>
<id>7</id>
<category>NONE</category>
<name>ServiceCharge</name>
<description>Service Charge</description>
<unitKey>NONE</unitKey>
<billingDetails>
    <billFrequency>NONE</billFrequency>
    <billingCycle>NOT_DEFINED</billingCycle>
</billingDetails>
<expiryDetails>
    <expiryDays>0</expiryDays>
    <expiryMethod>EXPIRE_PERIOD</expiryMethod>
</expiryDetails>
<gracePeriod>0</gracePeriod>
</item>

做这个java代码_

public static void digestItemsDetails(Digester digester, String root) {

String itemTag = root;
    digester.addObjectCreate(itemTag, Item.class);
    digester.addCallMethod(itemTag +"/id", "setId", 0);
    digester.addCallMethod(itemTag +"/category", "setCategory", 0);
    digester.addCallMethod(itemTag +"/name", "setName", 0);
    digester.addCallMethod(itemTag +"/description", "setDescription", 0); 
    digester.addCallMethod(itemTag +"/unitKey", "setUnitKey", 0);

    String billingPrefix = itemTag  + "/billingDetails";
    digester.addObjectCreate(billingPrefix, BillingDetails.class);
    digester.addCallMethod(billingPrefix +"/billFrequency", "setBillFrequency", 0);
    digester.addCallMethod(billingPrefix +"/billingCycle", "setBillingCycle", 0); 0); 

    String expiryPrefix = itemTag  + "/expiryDetails";
    digester.addObjectCreate(expiryPrefix, ExpiryDetails.class);
    digester.addCallMethod(expiryPrefix +"/expiryDays", "setExpiryDays", 0);
    digester.addCallMethod(expiryPrefix +"/expiryMethod", "setExpiryMethod", 0); 

    digester.addCallMethod(itemTag+"/gracePeriod", "setGracePeriod", 0); 
}

Item.java class

public class Item {
....
public BillingDetails getBillingDetails() {
    return billingDetails;
}
public void setBillingDetails(BillingDetails billingDetails) {
    this.billingDetails = billingDetails;
}
......
}

但是 billingDetails &amp; expiryDetails , anybuddy知道这有什么不对吗? 感谢名单

1 个答案:

答案 0 :(得分:2)

此测试有效。

import java.io.*;
import org.xml.sax.*;
import org.junit.*;
import org.apache.commons.digester.*;

import static org.junit.Assert.*;

public class DigesterTest {
    @Test
    public void testItemBillingDetailsNotNull() throws IOException, SAXException {
        Digester d = new Digester();
        MyApp.digestItemsDetails(d, "item");
        Item item = (Item) d.parse(DigesterTest.class.getResource("/test.xml"));
        assertNotNull(item.getBillingDetails());
        assertEquals("NONE", item.getBillingDetails().getBillFrequency());
        assertEquals("NOT_DEFINED", item.getBillingDetails().getBillingCycle());
    }
}

我认为您错过了对addSetNext()的调用:

import org.apache.commons.digester.*;

public class MyApp {
    public static void digestItemsDetails(Digester digester, String root) {
        String itemTag = root;
        digester.addObjectCreate(itemTag, Item.class);
        /* snip */
        String billingPrefix = itemTag  + "/billingDetails";
        digester.addObjectCreate(billingPrefix, BillingDetails.class);
        digester.addCallMethod(billingPrefix +"/billFrequency", "setBillFrequency", 0);
        digester.addCallMethod(billingPrefix +"/billingCycle", "setBillingCycle", 0);
        digester.addSetNext(billingPrefix, "setBillingDetails");
        /* snip */
    }
}