You might want to have a look at the following sample. It does have namespace 
defined for root element.

You can build this string as you like with more child routes.

                CamelContext context = new DefaultCamelContext();
                context.setTracing(true);

                String xmlString = "<routes  
xmlns=\"http://camel.apache.org/schema/spring\";><route><from 
uri='direct:c'/><to uri='mock:d'/></route></routes>";

                InputStream is = new ByteArrayInputStream(xmlString.getBytes());
                RoutesDefinition routes = context.loadRoutesDefinition(is);
                context.addRouteDefinitions(routes.getRoutes());

                context.start();

                ProducerTemplate template = null;
                template = context.createProducerTemplate();
                template.start();
                template.sendBody("direct:c", "HelloC");
                Thread.sleep(10000);
                context.stop();



-----Original Message-----
From: giacomolm [mailto:giacomo.lamon...@tirasa.net]
Sent: Friday, January 24, 2014 4:26 PM
To: users@camel.apache.org
Subject: Convert a Spring xml route string in a RouteDefinition

Hi all,

I saw that camel can load route from xml [1]: very useful functionality. Now
i'm trying to define a route starting only from a raw string consisting of a
single route. In example, suppose we have a string (compliant to Spring xml)
like this:

"<route id='id1'>
   <from uri='entrypoint'/>
   <to uri='exitpoint'>
</route>"

In other words, my goal is to obtain a RouteDefinition Object starting from
this xml string. So, i'm trying to replicate the same behaviour defined
inside the loadRouteDefinitions method of DefaultCamelContext class. The
process is something like this:

String xmlString= ... //the string above

//Definition of route Node Element
Document doc = dBuilder.parse((new InputSource(new
ByteArrayInputStream(xmlString.getBytes()))));
doc.getDocumentElement().normalize();
Node routeEl = doc.getElementsByTagName("route").item(0);

//unmarshalling process
JAXBContext jaxbContext =
JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES,
CamelContext.class.getClassLoader());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement  obj = unmarshaller.unmarshal(routeEl, RouteDefinition.class);
RouteDefinition answer = obj.getValue();

//camel context
camelContext.addRouteDefiniton(answer);
camelContext.start() // or camelContext can already been started

when camel (re)load the context with this new route definition, i receive a
NoSuchElementException. More specifically, after the unmarshiling process i
receive a route like this: [Route(id1)[[]->[]]. It seems that the route
content is empty. When i add the route to camel context, i receive the
NoSuchElementException discussed above.

In my opinion, there is something wrong in the unmarshalling process: JAXB
only recognize the root element (in this case the route element), while the
child element are not included in the result. Maybe the xml string doesn't
include information like namespace, and JAXB cannot proceed with a right
unmarshalling.

[1] file http://camel.apache.org/loading-routes-from-xml-files.html



--
View this message in context: 
http://camel.465427.n5.nabble.com/Convert-a-Spring-xml-route-string-in-a-RouteDefinition-tp5746443.html
Sent from the Camel - Users mailing list archive at Nabble.com.
This e-mail and any files transmitted with it are for the sole use of the 
intended recipient(s) and may contain confidential and privileged information. 
If you are not the intended recipient(s), please reply to the sender and 
destroy all copies of the original message. Any unauthorized review, use, 
disclosure, dissemination, forwarding, printing or copying of this email, 
and/or any action taken in reliance on the contents of this e-mail is strictly 
prohibited and may be unlawful.

Reply via email to