|
Re: [Moxy] How to use dynamic Moxy for specific example [message #1058312 is a reply to message #1058256] |
Mon, 13 May 2013 14:26 |
|
Quote:1st Do i need jaxb.properties file? if yes where to put it (for the xsd mentioned above)?
If you want to use that standard APIs for bootstrapping the JAXBContext then yes you need to include a jaxb.properties file. The package this goes in corresponds to the package name you will use to create the JAXBContext. For example if you create the JAXBContext like JAXBContext.newInstance("com.example.foo") then you need to place the jaxb.properties file in com/example/foo.
Quote:2nd How to reference/get/set the elements for this specific xsd.
EclipseLink's Dynamic JAXB will produce a model that closely mirrors what the corresponding static model would be. This means if the static model has a property called "foo", then you will be able to do a get/set passing in "foo" as the property name.
Quote:3rd How to get deeper elements?
Similar to static JAXB you can use a series of get methods to access deeper and deeper layers. Or you can cast the Context to the underlying implementation (org.eclipse.persistence.jaxb.JAXBHelper.getJAXBContext(jaxbContext)) and utilize the getValueByXPath method.
Quote:4th How to handle Lists
The values of any dynamic properties corresponding to a element with minOccurs greater than 1 will contain a List value. You will be able to cast the value to a List and interact with that.
For More Information
You can find some dynamic MOXy examples at the link below. I am going to try to extend these based on your questions.
- http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic
|
|
|
|
Re: [Moxy] How to use dynamic Moxy for specific example [message #1058388 is a reply to message #1058327] |
Mon, 13 May 2013 20:04 |
|
Here is some sample code that should help:
Demo Code
- When bootstrapping from an XML schema I would recommend using MOXy's native APIs as I have below.
- The get/set method parameters are based on what the properties would be in the generated classes. See below instead of using the element name "response_timestamp", we use the property name "responseTimestamp".
package may13;
import java.io.*;
import java.math.BigInteger;
import javax.xml.bind.*;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.*;
public class Demo {
public static void main(String[] args) throws Exception {
FileInputStream xsdInputStream = new FileInputStream("src/may13/generalInvoiceResponse_430.xsd");
DynamicJAXBContext jaxbContext =
DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
Unmarshaller unmarshaller= jaxbContext.createUnmarshaller();
File xml = new File("src/may13/input.xml");
JAXBElement<DynamicEntity> result = (JAXBElement<DynamicEntity>) unmarshaller.unmarshal(xml);
BigInteger responseTimestamp = result.getValue().<DynamicEntity>get("payload").<BigInteger>get("responseTimestamp");
System.out.println(responseTimestamp);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(result, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<ns:response xmlns:ns="http://www.forum-datenaustausch.ch/invoice" language="fr">
<ns:processing>
<ns:transport to="" from=""></ns:transport>
</ns:processing>
<ns:payload response_timestamp="12345">
<ns:credit request_date="2012-05-13T01:02:03" request_timestamp="67890" request_id="abc"/>
</ns:payload>
</ns:response>
Output
12345
<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="http://www.forum-datenaustausch.ch/invoice" language="fr">
<processing>
<transport from="" to=""/>
</processing>
<payload response_timestamp="12345">
<credit request_timestamp="67890" request_date="2012-05-13T01:02:03" request_id="abc"/>
</payload>
</response>
|
|
|
Re: [Moxy] How to use dynamic Moxy for specific example [message #1058450 is a reply to message #1058388] |
Tue, 14 May 2013 07:39 |
moritz du Messages: 102 Registered: February 2010 |
Senior Member |
|
|
Thank you thats really great.
But some Questions remain: The examples for those responses look slightly different. All Elements are prefixed with "invoice" instead of "ns". How to get a similar result (see attachment "Response430_Accepted01.xml")?
Because of many constant attributes (the complete xml-header is allways the same) i generated templates (with eclipse) for each format and stored them in resource-folder. I want to use them as starting point (Attachment generalInvoiceResponse_430.xml).
Does it mean i only have to prefix all element names by "invoice."?
(analog to "mynamespace" in moxy-doc(pdf Example 9-3): "DynamicEntity address = dynamicJAXBContext.newDynamicEntity("mynamespace.Address");")
Refresh for question one: i put the jaxb.properties in same package as the class that uses jaxb?
"what the properties would be in the generated classes" is there a way to know this without generating the classes (not only the names but also the types)?
[Updated on: Tue, 14 May 2013 08:21] Report message to a moderator
|
|
|
Re: [Moxy] How to use dynamic Moxy for specific example [message #1058563 is a reply to message #1058450] |
Tue, 14 May 2013 13:23 |
|
Quote:The examples for those responses look slightly different. All Elements are prefixed with "invoice" instead of "ns".
In the input.xml and output have the same namespace qualification (all the elements and attributes have the same local name and namespace), they just use different prefixes. I purposefully made the input.xml use different prefixes different from those used in the output to show that it doesn't affect how you access the data.
Quote:Refresh for question one: i put the jaxb.properties in same package as the class that uses jaxb?
When bootstrapping MOXy's dynamic JAXB from an XML schema I think it's best to use the native APIs which means you do not need to use a jaxb.properties file.
FileInputStream xsdInputStream = new FileInputStream("src/may13/generalInvoiceResponse_430.xsd");
DynamicJAXBContext jaxbContext =
DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
Quote:"what the properties would be in the generated classes" is there a way to know this without generating the classes (not only the names but also the types)?
Yes, below is an example of how you can interrogate the type of a dynamic entity:
DynamicEntity responseEntity = result.getValue();
DynamicType responseType = DynamicHelper.getType(responseEntity);
for(String propertyName : responseType.getPropertiesNames()) {
System.out.println(propertyName);
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.05756 seconds