Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » [Moxy] How to use dynamic Moxy for specific example
[Moxy] How to use dynamic Moxy for specific example [message #1058256] Mon, 13 May 2013 11:44 Go to next message
moritz du is currently offline moritz du
Messages: 98
Registered: February 2010
Member
I have some problems to map the existing dynamic Moxy tutorials to my sepcific use case.
I have read so far http://www.eclipse.org/eclipselink/documentation/2.4/eclipselink_moxy.pdf
and
http://blog.bdoughan.com/2013/04/moxys-object-graphs-dynamic-jaxb.html

I want to use dynamic Jaxb to create xml of different "response types" from here. For example: http://www.forum-datenaustausch.ch/generalinvoiceresponse_430.xsd.zip

Here are my questions:
1st Do i need jaxb.properties file? if yes where to put it (for the xsd mentioned above)?

2nd How to reference/get/set the elements for this specific xsd. Or how should the strings look like. Since the targetNamespace is "http://www.forum-datenaustausch.ch/invoice" does this mean i get the response element by "http://www.forum-datenaustausch.ch/invoice.response"?

3rd How to get deeper elements? this way?: "http://www.forum-datenaustausch.ch/invoice.response.payload.invoice"

4th How to handle Lists. In the model mentioned above, "http://www.forum-datenaustausch.ch/invoice.response.processing.transport" could contain many "via" elements. How to get the list or how to set additional entries in this list?

Thx in advance

[Updated on: Mon, 13 May 2013 13:01]

Report message to a moderator

Re: [Moxy] How to use dynamic Moxy for specific example [message #1058312 is a reply to message #1058256] Mon, 13 May 2013 14:26 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

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 #1058327 is a reply to message #1058312] Mon, 13 May 2013 14:52 Go to previous messageGo to next message
moritz du is currently offline moritz du
Messages: 98
Registered: February 2010
Member
Thx Blaise (i hoped you would answer the question Smile)

3 and 4 are clear but 1 and 2 are still open Sad :

for 1st
the docs i mentioned create the JAXBContext this way:
DynamicJAXBContext jaxbContext = 
            DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);

(there is no String for the package)
In my case the xsd "targetNamespace" is "http://www.forum-datenaustausch.ch/invoice" and "xmlns:invoice="http://www.forum-datenaustausch.ch/invoice".

So how to create the JAXBContext for my concrete xsd. Or where to put jaxb.properties (how to create a package from "www.forum-datenaustausch.ch/invoice"?)?

for 2nd
how does a concrete set/get looks like in my case- i don't know how to prefix the elements. get("response").get("payload") ... will not work? I think the element names must be prefied by namespace somehow?

So could you please provide a simple sample of code, creating the context and getting one element - for this concrete xsd (attached it to this reply)

[Updated on: Mon, 13 May 2013 14:53]

Report message to a moderator

Re: [Moxy] How to use dynamic Moxy for specific example [message #1058388 is a reply to message #1058327] Mon, 13 May 2013 20:04 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

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 Go to previous messageGo to next message
moritz du is currently offline moritz du
Messages: 98
Registered: February 2010
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 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

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);
        }

Re: [Moxy] How to use dynamic Moxy for specific example [message #1058583 is a reply to message #1058563] Tue, 14 May 2013 13:56 Go to previous message
moritz du is currently offline moritz du
Messages: 98
Registered: February 2010
Member
Thanks again!

That should get me started Smile
Previous Topic:JPA database schema validation case problems in ClassDescriptor#checkDatabase(...)
Next Topic:how to return value in anonymous pl sql using eclipselink
Goto Forum:
  


Current Time: Sat Oct 25 10:19:45 GMT 2014

Powered by FUDForum. Page generated in 0.02230 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software