You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by alapaka <al...@rocketmail.com> on 2015/04/10 08:50:31 UTC

castor unmarshal without mapping file

I am trying to unmarshal XML to java using castor. AFAIK castor can unmarshal
XML without a mapping file - as per the castor documentation and as
demonstrated by the example code in Camel documentation.

To test this I want to read an xml file, unmarshal it and place the
resulting object into a queue.

*This is the xml file:*

<mytest>
   <firstnode>this is node 1</firstnode>
   <secondnode>this is node 2</secondnode>
</mytest>

*This is the route:*

<camel:route id="test-unmarshal" autoStartup="true">
	<camel:from uri="file:C:/dev/testdata/unmarshal"/>
	<camel:log message="xml unmarshal to castor starting."/>
	<camel:unmarshal>
		<camel:castor/>
	</camel:unmarshal>
	<camel:log message="xml unmarshal to castor finished."/>
	<camel:to uri="activemq://outgoing.test.unmarshaled"/>
</camel:route>

*however, I get an exception as follows:*

org.exolab.castor.xml.MarshalException: The class for the root element
'mytest' could not be found.{File: [not available]; line: 2; column: 10}
        at
org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:866)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:763)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:629)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at
org.apache.camel.dataformat.castor.AbstractCastorDataFormat.unmarshal(AbstractCastorDataFormat.java:76)[215:org.apache.camel.camel-castor:2.13.2]
        at
org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:67)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.Pipeline.process(Pipeline.java:118)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:51)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.SendProcessor.process(SendProcessor.java:113)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:423)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:211)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:175)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:187)[124:org.apache.camel.camel-core:2.13.2]
        at
org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:114)[124:org.apache.camel.camel-core:2.13.2]
        at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)[:1.7.0_55]
        at
java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)[:1.7.0_55]
        at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)[:1.7.0_55]
        at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.7.0_55]
        at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_55]
        at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_55]
        at java.lang.Thread.run(Thread.java:745)[:1.7.0_55]
Caused by: org.xml.sax.SAXException: The class for the root element 'mytest'
could not be found.
        at
org.exolab.castor.xml.UnmarshalHandler.processFirstElement(UnmarshalHandler.java:890)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at
org.exolab.castor.xml.StartElementProcessor.compute(StartElementProcessor.java:103)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at
org.exolab.castor.xml.UnmarshalHandler.startElementProcessing(UnmarshalHandler.java:811)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at
org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:733)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
        at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
Source)[:]
        at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown
Source)[:]
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
Source)[:]
        at
org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown
Source)[:]
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)[:]
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)[:]
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)[:]
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)[:]
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)[:]
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
Source)[:]
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
Source)[:]
        at
org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:751)[214:org.apache.servicemix.bundles.castor:1.3.2.2]


any ideas?



--
View this message in context: http://camel.465427.n5.nabble.com/castor-unmarshal-without-mapping-file-tp5765569.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: castor unmarshal without mapping file

Posted by alapaka <al...@rocketmail.com>.
I decided to RTFM - looks like your answer is spot on:

http://castor.codehaus.org/1.2/xml-mapping.html#2.2-Unmarshalling-Behavior


> When Castor finds an element while unmarshalling a document, it will try
> to use the mapping information to determine which object to instantiate.
> If no mapping information is present, Castor will use the name of the
> element to try to guess the name of a class to instantiate (for example,
> for an element named 'test-element', Castor will try to instantiate a
> class named 'TestElement' if no information is given in the mapping file).
> Castor will then use the field information of the mapping file to handle
> the content of the element.
> 
> If the class is not described in the mapping file, Castor will instrospect
> the class using the Java Reflection API to determine if there is any
> function of the form getXxxYyy()/setXxxYyy(
> <type>
>  x). This accessor will be associated with XML element/attribute named
> 'xxx-yyy'. In the future, we will provide a way to override this default
> behavior.
> 
> ...


I was hoping to find a xml ORM that would do all this on the fly without
need of mapping file and/or skeleton class; seems I am expecting too much,
and anyway i'm not sure how desireable this is from a dev perspective. 



--
View this message in context: http://camel.465427.n5.nabble.com/castor-unmarshal-without-mapping-file-tp5765569p5765666.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: castor unmarshal without mapping file

Posted by alapaka <al...@rocketmail.com>.
I *think* I understand what you are saying - however does that not equate to
a mapping file? 

I was hoping that castor would function without a mapping file and create a
default class structure, for example based on the element/attribute names in
the XML document.

I am a complete castor newb, so feel free to dumb it down for me. A little
sample would go a long way.
How for example do I "specify the template class structure of which the
object has to be created"?

aloha



--
View this message in context: http://camel.465427.n5.nabble.com/castor-unmarshal-without-mapping-file-tp5765569p5765665.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: castor unmarshal without mapping file

Posted by Reji Mathews <co...@gmail.com>.
When u unmarshal this xml you will get a Java object. You need to specify
the template class structure of which the object has to be created.

Cheers
Reji
On 10 Apr 2015 12:22, "alapaka" <al...@rocketmail.com> wrote:

> I am trying to unmarshal XML to java using castor. AFAIK castor can
> unmarshal
> XML without a mapping file - as per the castor documentation and as
> demonstrated by the example code in Camel documentation.
>
> To test this I want to read an xml file, unmarshal it and place the
> resulting object into a queue.
>
> *This is the xml file:*
>
> <mytest>
>    <firstnode>this is node 1</firstnode>
>    <secondnode>this is node 2</secondnode>
> </mytest>
>
> *This is the route:*
>
> <camel:route id="test-unmarshal" autoStartup="true">
>         <camel:from uri="file:C:/dev/testdata/unmarshal"/>
>         <camel:log message="xml unmarshal to castor starting."/>
>         <camel:unmarshal>
>                 <camel:castor/>
>         </camel:unmarshal>
>         <camel:log message="xml unmarshal to castor finished."/>
>         <camel:to uri="activemq://outgoing.test.unmarshaled"/>
> </camel:route>
>
> *however, I get an exception as follows:*
>
> org.exolab.castor.xml.MarshalException: The class for the root element
> 'mytest' could not be found.{File: [not available]; line: 2; column: 10}
>         at
>
> org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:866)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at
>
> org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:763)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at
>
> org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:629)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at
>
> org.apache.camel.dataformat.castor.AbstractCastorDataFormat.unmarshal(AbstractCastorDataFormat.java:76)[215:org.apache.camel.camel-castor:2.13.2]
>         at
>
> org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:67)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.Pipeline.process(Pipeline.java:118)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:51)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.SendProcessor.process(SendProcessor.java:113)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:423)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:211)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:175)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:187)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:114)[124:org.apache.camel.camel-core:2.13.2]
>         at
>
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)[:1.7.0_55]
>         at
> java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)[:1.7.0_55]
>         at
>
> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)[:1.7.0_55]
>         at
>
> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.7.0_55]
>         at
>
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_55]
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_55]
>         at java.lang.Thread.run(Thread.java:745)[:1.7.0_55]
> Caused by: org.xml.sax.SAXException: The class for the root element
> 'mytest'
> could not be found.
>         at
>
> org.exolab.castor.xml.UnmarshalHandler.processFirstElement(UnmarshalHandler.java:890)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at
>
> org.exolab.castor.xml.StartElementProcessor.compute(StartElementProcessor.java:103)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at
>
> org.exolab.castor.xml.UnmarshalHandler.startElementProcessing(UnmarshalHandler.java:811)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at
>
> org.exolab.castor.xml.UnmarshalHandler.startElement(UnmarshalHandler.java:733)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>         at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
> Source)[:]
>         at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown
> Source)[:]
>         at
>
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
> Source)[:]
>         at
>
> org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown
> Source)[:]
>         at
>
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
> Source)[:]
>         at
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
> Source)[:]
>         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
> Source)[:]
>         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
> Source)[:]
>         at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)[:]
>         at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
> Source)[:]
>         at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
> Source)[:]
>         at
>
> org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:751)[214:org.apache.servicemix.bundles.castor:1.3.2.2]
>
>
> any ideas?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/castor-unmarshal-without-mapping-file-tp5765569.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>