Ignore attribute when transforming Java to XML

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Ignore attribute when transforming Java to XML

Gaël Varlet
Hi,

I have got a Java Bean (let's say "Order", the famous example of Smooks) and I'd like to tranform it in XML. But, I'd like to ignore some attributes (like 'Header' for instance).
My bean is like that :

  public class Order{
    private Header header;
    private List<Items> items;

    //getters and setters
  }

My Main.class is :

...
        Smooks smooks = new Smooks("smooks-config-XMLtoJava2.xml");

        try {
            ExecutionContext executionContext = smooks.createExecutionContext();
            StringWriter writer = new StringWriter();

            // Filter the message to the result writer, using the execution context...
            smooks.filterSource(executionContext, new JavaSource(reunion), new StreamResult(writer));

            return writer.toString();
        } finally {
            smooks.close();
        }
...

and my configuration is :

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">

    <resource-config selector="global-parameters">
        DOM
    </resource-config>

        <resource-config selector="fr.smooks.modelInfocentre.Order">
    <resource>org.milyn.cdres.trans.RenameElementTU</resource>
    Order
    </resource-config>
       
</smooks-resource-list>

Can I ignore "Header" without modify my bean ?

Thanks a lot
Gael Varlet
Reply | Threaded
Open this post in threaded view
|

Re: Ignore attribute when transforming Java to XML

Tom Fennelly
The best way to convert Java to XML (or anything else) is to apply a
Freemarker template to it.

Regards,

Tom.

On 18/06/2013 14:37, Gaël Varlet wrote:

> Hi,
>
> I have got a Java Bean (let's say "Order", the famous example of Smooks) and
> I'd like to tranform it in XML. But, I'd like to ignore some attributes
> (like 'Header' for instance).
> My bean is like that :
>
>    public class Order{
>      private Header header;
>      private List<Items> items;
>
>      //getters and setters
>    }
>
> My Main.class is :
>
> ...
>          Smooks smooks = new Smooks("smooks-config-XMLtoJava2.xml");
>
>          try {
>              ExecutionContext executionContext =
> smooks.createExecutionContext();
>              StringWriter writer = new StringWriter();
>
>              // Filter the message to the result writer, using the execution
> context...
>              smooks.filterSource(executionContext, new JavaSource(reunion),
> new StreamResult(writer));
>
>              return writer.toString();
>          } finally {
>              smooks.close();
>          }
> ...
>
> and my configuration is :
>
> <?xml version="1.0"?>
> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">
>
>      <resource-config selector="global-parameters">
>          DOM
>      </resource-config>
>
> <resource-config selector="fr.smooks.modelInfocentre.Order">
>       <resource>org.milyn.cdres.trans.RenameElementTU</resource>
>       Order
>      </resource-config>
>
> </smooks-resource-list>
>
> Can I ignore "Header" without modify my bean ?
>
> Thanks a lot
>
>
>
> -----
> Gael Varlet
> --
> View this message in context: http://milyn.996300.n3.nabble.com/Ignore-attribute-when-transforming-Java-to-XML-tp8610.html
> Sent from the milyn - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Ignore attribute when transforming Java to XML

Gaël Varlet
Ok thank you,
so I've got another problem :


I want to create an XML like this

<Order>
   <Header>
      ...
   </Header>
   <ListOfValues1>
      <Value1>
         ...
      </Value1>
      <Value1>
         ...
      </Value1>
   </ListOfValues1>
   <ListOfValues2>
      <Value2>
         ...
      </Value2>
      <Value2>
         ...
      </Value2>
   </ListOfValues2>
</Order>


But I can't write my smooks config like that because I got an error  (note that I removed the characters < !--) :

 <ftl:freemarker applyOnElement="fr.smooks.Order">
        <ftl:template>
<Order>
             .....
        <ListOfValues1>
             <?TEMPLATE-SPLIT-PI?>
        </ListOfValues1>
        <ListOfValues2>
             <?TEMPLATE-SPLIT-PI?>
        </ListOfValues2>
</Order>
</ftl:template>
    </ftl:freemarker>

How can I do that ?

Thanks a lot
Gael Varlet