You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by dunnlow <du...@yahoo.com> on 2011/07/20 22:18:39 UTC

Reading XML file into a bean

I'd like to read XML files that will have some number of parameters defined
for a bean and ultimately store the information into a database.  

Currently I am reading from a JMS queue and using a toJdbc method on my bean
to create the sql to pass on to my database endpoint.  That works fine.  now
I want to pull the same parameters out of XML files.

It seems like this should be simple, but haven't had any luck.

my toJDBC looks like..

public String toJdbc(Exchange exchange){
    Map&lt;String,Object&gt; theHeaders = exchange.getIn().getHeaders();
    String theBody = exchange.getIn().getBody(String.class);
    StringBuilder sb = .... 
       8<....
    sb.append(((theHeaders.containsKey("age"))...
    &lt;build the rest of the jdbc string using theHeaders&gt;
     ...
    return sb.toString();

(but the problem seems to be with unmarshalling the XML file)

Things I've tried:
1) "*file://c:/infile).beanRef("myBean","toJdbc").to("stream:out")*;
    this grabs the file and loads the full XML text into the msg parameter
of my jdbc (that method loads exchange.getIn().getBody into the msg
parameter), which i suppose I could parse by hand.

2)
*file://c:/infile).marshal().xtream().convertBodyTo(com.me.MyBean.class).beanRef("myBean","toJdbc")*
    Hoping for some magic conversion, but no luck

3) I wrote a method for my bean with annotated XML parameters like:
    public void fromXML(@XPath("/account/option/value") String value,
   ....
   System.out.println("value="+value);

   But, value is always empty when i use the route:

   
*file://c:/infile).marshal().xtream().beanRef("myBean","fromXML").to("stream:out");*

So, my question is/are:
1) should #3 work?  

2) Isn't there a better way?

3) Ideally, i'd like to parse the xml that comes in and NOT assume the XPATH
is correct.  Do I need to do that myself?

Thanks!
-J

--
View this message in context: http://camel.465427.n5.nabble.com/Reading-XML-file-into-a-bean-tp4617160p4617160.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Reading XML file into a bean

Posted by Ashwin Karpe <ak...@fusesource.com>.
Hi,

Not sure where things are going wrong but in option 3 there should be no
need to marshal XML into Java before calling the method if you intend to use
XPath injection.

Please check out a straightforward working example of what you are looking
for given below. 

https://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanWithXPathInjectionTest.java?view=markup
https://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanWithXPathInjectionTest.java?view=markup 

Hope this helps you move forward...

Cheers,

Ashwin...

-----
---------------------------------------------------------
Ashwin Karpe
Apache Camel Committer & Sr Principal Consultant
FUSESource (a Progress Software Corporation subsidiary)
http://fusesource.com 

Blog: http://opensourceknowledge.blogspot.com 
---------------------------------------------------------
--
View this message in context: http://camel.465427.n5.nabble.com/Reading-XML-file-into-a-bean-tp4617160p4617564.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: Reading XML file into a bean

Posted by Narita Bagchi <Na...@headstrong.com>.
Some of the XPath APIs doesn't work if the XML is not formatted.


-----Original Message-----
From: Claus Ibsen [mailto:claus.ibsen@gmail.com]
Sent: Thursday, July 21, 2011 12:08 PM
To: users@camel.apache.org
Subject: Re: Reading XML file into a bean

When using XPath you should always mind namespaces. If your XML have a
namespace, then the XPath expression must have that namespace bound as
well.



On Wed, Jul 20, 2011 at 10:18 PM, dunnlow <du...@yahoo.com> wrote:
> I'd like to read XML files that will have some number of parameters defined
> for a bean and ultimately store the information into a database.
>
> Currently I am reading from a JMS queue and using a toJdbc method on my bean
> to create the sql to pass on to my database endpoint.  That works fine.  now
> I want to pull the same parameters out of XML files.
>
> It seems like this should be simple, but haven't had any luck.
>
> my toJDBC looks like..
>
> public String toJdbc(Exchange exchange){
>    Map<String,Object> theHeaders = exchange.getIn().getHeaders();
>    String theBody = exchange.getIn().getBody(String.class);
>    StringBuilder sb = ....
>       8<....
>    sb.append(((theHeaders.containsKey("age"))...
>    <build the rest of the jdbc string using theHeaders>
>     ...
>    return sb.toString();
>
> (but the problem seems to be with unmarshalling the XML file)
>
> Things I've tried:
> 1) "*file://c:/infile).beanRef("myBean","toJdbc").to("stream:out")*;
>    this grabs the file and loads the full XML text into the msg parameter
> of my jdbc (that method loads exchange.getIn().getBody into the msg
> parameter), which i suppose I could parse by hand.
>
> 2)
> *file://c:/infile).marshal().xtream().convertBodyTo(com.me.MyBean.class).beanRef("myBean","toJdbc")*
>    Hoping for some magic conversion, but no luck
>
> 3) I wrote a method for my bean with annotated XML parameters like:
>    public void fromXML(@XPath("/account/option/value") String value,
>   ....
>   System.out.println("value="+value);
>
>   But, value is always empty when i use the route:
>
>
> *file://c:/infile).marshal().xtream().beanRef("myBean","fromXML").to("stream:out");*
>
> So, my question is/are:
> 1) should #3 work?
>
> 2) Isn't there a better way?
>
> 3) Ideally, i'd like to parse the xml that comes in and NOT assume the XPATH
> is correct.  Do I need to do that myself?
>
> Thanks!
> -J
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Reading-XML-file-into-a-bean-tp4617160p4617160.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

***The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review,retransmission,dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.***

Re: Reading XML file into a bean

Posted by Claus Ibsen <cl...@gmail.com>.
When using XPath you should always mind namespaces. If your XML have a
namespace, then the XPath expression must have that namespace bound as
well.



On Wed, Jul 20, 2011 at 10:18 PM, dunnlow <du...@yahoo.com> wrote:
> I'd like to read XML files that will have some number of parameters defined
> for a bean and ultimately store the information into a database.
>
> Currently I am reading from a JMS queue and using a toJdbc method on my bean
> to create the sql to pass on to my database endpoint.  That works fine.  now
> I want to pull the same parameters out of XML files.
>
> It seems like this should be simple, but haven't had any luck.
>
> my toJDBC looks like..
>
> public String toJdbc(Exchange exchange){
>    Map<String,Object> theHeaders = exchange.getIn().getHeaders();
>    String theBody = exchange.getIn().getBody(String.class);
>    StringBuilder sb = ....
>       8<....
>    sb.append(((theHeaders.containsKey("age"))...
>    <build the rest of the jdbc string using theHeaders>
>     ...
>    return sb.toString();
>
> (but the problem seems to be with unmarshalling the XML file)
>
> Things I've tried:
> 1) "*file://c:/infile).beanRef("myBean","toJdbc").to("stream:out")*;
>    this grabs the file and loads the full XML text into the msg parameter
> of my jdbc (that method loads exchange.getIn().getBody into the msg
> parameter), which i suppose I could parse by hand.
>
> 2)
> *file://c:/infile).marshal().xtream().convertBodyTo(com.me.MyBean.class).beanRef("myBean","toJdbc")*
>    Hoping for some magic conversion, but no luck
>
> 3) I wrote a method for my bean with annotated XML parameters like:
>    public void fromXML(@XPath("/account/option/value") String value,
>   ....
>   System.out.println("value="+value);
>
>   But, value is always empty when i use the route:
>
>
> *file://c:/infile).marshal().xtream().beanRef("myBean","fromXML").to("stream:out");*
>
> So, my question is/are:
> 1) should #3 work?
>
> 2) Isn't there a better way?
>
> 3) Ideally, i'd like to parse the xml that comes in and NOT assume the XPATH
> is correct.  Do I need to do that myself?
>
> Thanks!
> -J
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Reading-XML-file-into-a-bean-tp4617160p4617160.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Reading XML file into a bean

Posted by dunnlow <du...@yahoo.com>.
The issue WAS with my XPath/name parsing.  I ended up using method #3 and as
Ashwin mentioned, I did not need to marshall.  So the following works:

from("file://c:/indir").beanRef("MyBean","fromXML").beanRef("myBean","toJdbc").to("jdbc:dataSource");

My fromXML method loads the Headers so that my toJDBC method can be used. 
*Interestingly*, I tried using the @Headers annotation thinking that would
give me the object that I needed to set, but it was always null.  I needed
to use exchange.getIn().getHeaders() instead.

of course, this solution lead me to a new issue, but that's for another
thread :)

Thanks!

--
View this message in context: http://camel.465427.n5.nabble.com/Reading-XML-file-into-a-bean-tp4617160p4619784.html
Sent from the Camel - Users mailing list archive at Nabble.com.