I am new to Apache Camel and tested us, and here it is ...
I have XML without an XSD schema, which I do not affect. The XML child data element items that I want to bind to my pojo business. This POJO (WeatherCurrent) is already annotated by JPA, and I was thinking of adding JAXB annotation, so the broken XML could be mapped to my POJO.
Since this XML has a root element and I only want its childs (metData), I have a problem how to annotate my POJO, since I cannot use @XmlRootElement.
This is partially described here: http://camel.apache.org/splitter.html when streaming large XML payloads using the chapter of the Tokenizer language. My POJO looks like an xml order element in this example. I need only a few elements from the xml metData element to map to POJO fields.
There is also a Partial marshalling / unmarshalling section at http://camel.apache.org/jaxb.html , but there is no JAVA DSL example (required), as well as how to annotate pojo's work with XML fragments.
So far I have this test code:
import java.io.File; import org.apache.camel.EndpointInject; import org.apache.camel.Exchange; import org.apache.camel.ProducerTemplate; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.converter.jaxb.JaxbDataFormat; import org.apache.camel.spi.DataFormat; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; public class WeatherCurrentTest extends CamelTestSupport { @EndpointInject(uri = "file:src/test/resources") private ProducerTemplate inbox; @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { DataFormat jaxbDataFormat = new JaxbDataFormat("com.mycompany.model.entities.weather");
XML (observ_si_latest.xml) is supplied in the form:
<?xml version="1.0" encoding="UTF-8"?> <data id="MeteoSI_WebMet_observation_xml"> <language>sl</language> <metData> <domain_altitude>55</domain_altitude> <domain_title>NOVA GORICA</domain_title> <domain_shortTitle>BILJE</domain_shortTitle> <tsValid_issued>09.03.2012 15:00 CET</tsValid_issued> <t_degreesC>15</t_degreesC> </metData> <metData> <domain_meteosiId>KREDA-ICA_</domain_meteosiId>
For brevity, I have left many elements of metData elements. I want to map (among others) domain_title to my JPA-annotated POJO station field, and then save its database, hopefully all in one smart and short Camel route.
POJO (no JAXB annotations yet):
@Entity @Table(name="weather_current") public class WeatherCurrent implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String station; @Temporal( TemporalType.TIMESTAMP) @Column(name="successfully_updated") private Date successfullyUpdated; private short temperature; @Column(name="wind_direction") private String windDirection; }
I also left many fields and methods.
So, the idea is to map the value of the * domain_title * field to the WeatherCurrent POJO field and do this for each metData element and save the list of WeatherCurrent objects in the database.
Any advice on how to implement this is welcome.