I think this is a great idea also. But I am having trouble getting it to work.
I wrote the following class:
public class UnitPrice {
| public double value;
| UnitPrice(double value){
| this.value=value;
| }
| UnitPrice()
| {
|
| }
| public double getValue()
| {
| return this.value;
| }
|
| }
with the following config file: (note I've tried the XmlAttribute and the commented
out XmlElement for presenting a UnitPrice and its value.)
| <?xml version = "1.0" encoding = "UTF-8"?>
| <jaxb-intros
xmlns="http://www.jboss.org/xsd/jaxb/intros">
| <Class name="junit.prices.UnitPrice">
| <XmlType name = "UnitPrice"/>
| <Field name="value">
| <!-- XmlElement name="value" nillable="true" /-->
| <XmlAttribute name="value" required="true" />
| </Field>
| </Class>
| </jaxb-intros>
|
and the following xml file for unmarshalling:
<?xml version="1.0" encoding="UTF-8"?>
| <UnitPrice value="40.0"
xsi:noNamespaceSchemaLocation="C:\UnitPrice.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
|
I then used the following code to test the unmarshalling of the above XML.
JaxbIntros config =
IntroductionsConfigParser.parseConfig(getClass().getResourceAsStream("UnitConfig.xml"));
|
|
| ClassIntroConfig classIntroConfig = config.getClazz().get(0);
| assertEquals(UnitPrice.class.getName(), classIntroConfig.getName());
| IntroductionsAnnotationReader reader = new
IntroductionsAnnotationReader(config);
|
| Map<String, Object> jaxbConfig= new HashMap<String, Object>();
| jaxbConfig.put(JAXBRIContext.ANNOTATION_READER, reader);
| JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {UnitPrice.class},
jaxbConfig);
|
| Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
| JAXBElement jbe = null;
| UnitPrice order =null;
|
| StreamSource ss = new
StreamSource(getClass().getResourceAsStream("UsdUnitPrice1.xml"));
|
| jbe = unmarshaller.unmarshal(ss, UnitPrice.class);
|
| order =(UnitPrice)jbe.getValue();
| try{
| assertEquals("get double value error", 40.0, order.getValue());
| } catch (Exception e) {
| fail(e.getMessage());
| }
|
a UnitPrice instance is instantiated. However, the above test code fails because the value
attribute of the unmarshalled UnitPrice instance is 0.0 instead of 40.0.
Does anyone see any problems with the JAXB Intros config file above that would cause JAXB
to fail to properly unmarshall the above XML file?
Thanks,
Sylvia
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4073112#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...