java - camel route: read xml into pojo and write it back into xml file -


i looging time now, since lot of configuration files in xml it's hard find answers question.

what do? using caml route want read in xml file , put pojo. here want analyze it. @ end want write different xml file (pojo) answer out folder.

my problem is, don't know how tell camel parse xml file body pojo.

a short example did until know:

my camel route:

from("file:data/in")                     .marshal().xstream()                     .bean(xmltobeanandbackbean.class)                     .unmarshal().xstream()                     .to("file:data/out"); 

my pojo:

@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class xmlfilepojo {       @xmlanyattribute      private string name;      @xmlelement(name = "the_age")      private int theage;       public void setname(string name) {          this.name = name;      } } 

and bean used in camel route:

@component public class xmltobeanandbackbean {      public xmlfilepojo transformxmlobject(xmlfilepojo xmlfilepojo){         xmlfilepojo returnpojo = xmlfilepojo;         returnpojo.setname("merkur");         return returnpojo;     } } 

i think error in camel route camel trys converting xml file xmlfilepojo object.

when try run following error:

caused by: org.apache.camel.invalidpayloadexception: no body available of type: xmlfilepojo has value: [b@659392cd of type: byte[] on: simple.xml. caused by: no type converter available convert type: byte[] required type: xmlfilepojo value [b@659392cd. exchange[simple.xml]. caused by: [org.apache.camel.notypeconversionavailableexception - no type converter available convert type: byte[] required type: xmlfilepojo value [b@659392cd]

since don't have byte[] in file don't know how handle this. hope has answer.

just add camel-jaxb classpath , can automatic xml <--> pojo conversition, when using jaxb annotations on pojos. write bean code using pojos.

then route simple

from("file:data/in")     .bean(xmltobeanandbackbean.class)     .to("file:data/out"); 

Comments