2

我正在对 java 对象进行 xml 解析,但解析时间正在获取billingDetailsexpiryDetails *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

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

但是我在billingDetailsexpiryDetails中得到 NULL ,有没有朋友知道这有什么问题?谢谢

4

1 回答 1

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 */
    }
}
于 2013-05-04T08:33:52.550 回答