You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by hutao722 <hu...@163.com> on 2013/09/16 06:40:06 UTC
JAXB partial unmarshalling
Hi,
I tried to do JAXB partial unmarshalling test, but always get null values
from xml to java object. The code like this:
JaxbDataFormat jaxb = new JaxbDataFormat(false);
jaxb.setContextPath("org.apache.camel.example.server.model6");
jaxb.setPartClass("org.apache.camel.example.server.model6.MobilePhone");
from("direct:example")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<Result
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"Yangzhili\">");
sb.append("<MobilePhone>");
sb.append("<QueryResult>success</QueryResult>");
sb.append("<AreaCode>100000</AreaCode>");
sb.append("</MobilePhone>");
sb.append("</Result>");
exchange.getIn().setBody(sb.toString());
}
})
.unmarshal(jaxb)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
MobilePhone sq = exchange.getIn().getBody(MobilePhone.class);
if (sq.getQueryResult() != null)
System.out.println("result is : " + sq.toString());
}
});
The class of MobilePhone:
@XmlRootElement(name="MobilePhone")
@XmlAccessorType(XmlAccessType.FIELD)
public class MobilePhone {
@XmlElement
private String QueryResult;
@XmlElement
private String AreaCode;
}
The values of QueryResult & AreaCode are always null. Why? Do i have some
wrong?
--
View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: JAXB partial unmarshalling
Posted by hutao722 <hu...@163.com>.
Thank you for your explain, Willem. But i read from the link
http://camel.apache.org/jaxb.html
it says:
JAXB 2 supports marshalling and unmarshalling XML tree fragments. By default
JAXB looks for @XmlRootElement annotation on given class to operate on whole
XML tree. This is useful but not always - sometimes generated code does not
have @XmlRootElement annotation, *sometimes you need unmarshall only part of
tree*.
In that case you can use partial unmarshalling. To enable this behaviours
you need set property partClass. Camel will pass this class to JAXB's
unmarshaler.
i thought the partial unmarshalling can unmarshall the only part of tree.
Now my question is that if i have a big SOAP response and i only wish to
focus on just small pieces, as you said, i need to travel the xml tree by
dom or sax and skip the unnecessary content, right? This may have a lot of
work to do, do we have better ways?
Thanks!
--
View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497p5739509.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: JAXB partial unmarshalling
Posted by Willem jiang <wi...@gmail.com>.
The Partial unmarshalling just works for the element start with <MobilePhone>.
You have to skip the <Result> part yourself.
--
Willem Jiang
Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem
On Monday, September 16, 2013 at 12:40 PM, hutao722 wrote:
> Hi,
>
> I tried to do JAXB partial unmarshalling test, but always get null values
> from xml to java object. The code like this:
>
> JaxbDataFormat jaxb = new JaxbDataFormat(false);
> jaxb.setContextPath("org.apache.camel.example.server.model6");
> jaxb.setPartClass("org.apache.camel.example.server.model6.MobilePhone");
> from("direct:example")
> .process(new Processor() {
> @Override
> public void process(Exchange exchange) throws Exception {
> StringBuffer sb = new StringBuffer();
> sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
> sb.append("<Result
> xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"Yangzhili\">");
> sb.append("<MobilePhone>");
> sb.append("<QueryResult>success</QueryResult>");
> sb.append("<AreaCode>100000</AreaCode>");
> sb.append("</MobilePhone>");
> sb.append("</Result>");
> exchange.getIn().setBody(sb.toString());
> }
> })
> .unmarshal(jaxb)
> .process(new Processor() {
> @Override
> public void process(Exchange exchange) throws Exception {
> MobilePhone sq = exchange.getIn().getBody(MobilePhone.class);
> if (sq.getQueryResult() != null)
> System.out.println("result is : " + sq.toString());
> }
> });
>
> The class of MobilePhone:
> @XmlRootElement(name="MobilePhone")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class MobilePhone {
> @XmlElement
> private String QueryResult;
> @XmlElement
> private String AreaCode;
> }
>
> The values of QueryResult & AreaCode are always null. Why? Do i have some
> wrong?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).
Re: JAXB partial unmarshalling
Posted by contactreji <co...@gmail.com>.
Hi
try implementing Serializable class
like
public class MobilePhone extends Serializable {
@XmlElement
private String QueryResult;
@XmlElement
private String AreaCode;
}
--
View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497p5741846.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: JAXB partial unmarshalling
Posted by hutao722 <hu...@163.com>.
Thanks, Macro. I tried, but it still doesn't work.
--
View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497p5739524.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: JAXB partial unmarshalling
Posted by hutao722 <hu...@163.com>.
Thank you all. I tried many times, but the partial unmarshalling still failed
for me. Instead, the Claus's two blogs bring me another two solutions. They
both work for me.
http://www.davsclaus.com/2011/11/splitting-big-xml-files-with-apache.html
http://www.davsclaus.com/2011/11/splitting-big-xml-files-with-apache_24.html
Thanks!
--
View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497p5739576.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: JAXB partial unmarshalling
Posted by hutao722 <hu...@163.com>.
I think the ns is no problem although it is indeed an invalid ns. It is
because If i use the MobilePhone as the root element, the unmarshal
operation is OK. But it failed when I try to do partial unmarshal.
Do you have some correct examples I can follow or some useful info about
this topic? Thanks!
--
View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497p5739547.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: JAXB partial unmarshalling
Posted by Marco Westermann <Ma...@gmx.de>.
In addition to my last anwer: Are you sure your namespaces are correct?
your xml is in the namespace: Yangzhili
regards, Marco
Am 16.09.2013 11:50, schrieb Marco Westermann:
> Hi,
>
> in addition to what you do I alway set the fragement property to true.
> Try the follwing:
>
> jaxb.setFragment(true)
>
> regards, Marco
>
> Am 16.09.2013 06:40, schrieb hutao722:
>> Hi,
>>
>> I tried to do JAXB partial unmarshalling test, but always get null
>> values
>> from xml to java object. The code like this:
>>
>> JaxbDataFormat jaxb = new JaxbDataFormat(false);
>> jaxb.setContextPath("org.apache.camel.example.server.model6");
>> jaxb.setPartClass("org.apache.camel.example.server.model6.MobilePhone");
>> from("direct:example")
>> .process(new Processor() {
>> @Override
>> public void process(Exchange exchange) throws Exception {
>> StringBuffer sb = new StringBuffer();
>> sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
>> sb.append("<Result
>> xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
>> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"Yangzhili\">");
>> sb.append("<MobilePhone>");
>> sb.append("<QueryResult>success</QueryResult>");
>> sb.append("<AreaCode>100000</AreaCode>");
>> sb.append("</MobilePhone>");
>> sb.append("</Result>");
>> exchange.getIn().setBody(sb.toString());
>> }
>> })
>> .unmarshal(jaxb)
>> .process(new Processor() {
>> @Override
>> public void process(Exchange exchange) throws Exception {
>> MobilePhone sq = exchange.getIn().getBody(MobilePhone.class);
>> if (sq.getQueryResult() != null)
>> System.out.println("result is : " + sq.toString());
>> }
>> });
>>
>> The class of MobilePhone:
>> @XmlRootElement(name="MobilePhone")
>> @XmlAccessorType(XmlAccessType.FIELD)
>> public class MobilePhone {
>> @XmlElement
>> private String QueryResult;
>> @XmlElement
>> private String AreaCode;
>> }
>>
>> The values of QueryResult & AreaCode are always null. Why? Do i have
>> some
>> wrong?
>>
>>
>>
>> --
>> View this message in context:
>> http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>
Re: JAXB partial unmarshalling
Posted by Marco Westermann <Ma...@gmx.de>.
Hi,
in addition to what you do I alway set the fragement property to true.
Try the follwing:
jaxb.setFragment(true)
regards, Marco
Am 16.09.2013 06:40, schrieb hutao722:
> Hi,
>
> I tried to do JAXB partial unmarshalling test, but always get null values
> from xml to java object. The code like this:
>
> JaxbDataFormat jaxb = new JaxbDataFormat(false);
> jaxb.setContextPath("org.apache.camel.example.server.model6");
> jaxb.setPartClass("org.apache.camel.example.server.model6.MobilePhone");
> from("direct:example")
> .process(new Processor() {
> @Override
> public void process(Exchange exchange) throws Exception {
> StringBuffer sb = new StringBuffer();
> sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
> sb.append("<Result
> xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
> xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"Yangzhili\">");
> sb.append("<MobilePhone>");
> sb.append("<QueryResult>success</QueryResult>");
> sb.append("<AreaCode>100000</AreaCode>");
> sb.append("</MobilePhone>");
> sb.append("</Result>");
> exchange.getIn().setBody(sb.toString());
> }
> })
> .unmarshal(jaxb)
> .process(new Processor() {
> @Override
> public void process(Exchange exchange) throws Exception {
> MobilePhone sq = exchange.getIn().getBody(MobilePhone.class);
> if (sq.getQueryResult() != null)
> System.out.println("result is : " + sq.toString());
> }
> });
>
> The class of MobilePhone:
> @XmlRootElement(name="MobilePhone")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class MobilePhone {
> @XmlElement
> private String QueryResult;
> @XmlElement
> private String AreaCode;
> }
>
> The values of QueryResult & AreaCode are always null. Why? Do i have some
> wrong?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/JAXB-partial-unmarshalling-tp5739497.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>