我尝试了很多方法。但找不到解决办法。请指导我获得正确的工作代码。这里不向 csv 文件添加值。
import org.apache.camel.builder.RouteBuilder;
import javax.xml.bind.JAXBContext;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.spi.DataFormat;
import com.infogain.gcp.camel.animal.AnumalCsv;
public class XmlRouter extends RouteBuilder {
private static final String INBOX = "file:data/inbox?noop=true&include=.*.xml";
@Override
public void configure() throws Exception {
JAXBContext context = JAXBContext.newInstance(new Class[] { com.gcp.camel.animal.Animals.class });
JaxbDataFormat xmlDataFormat = new JaxbDataFormat();
xmlDataFormat.setContext(context);
DataFormat csv = new BindyCsvDataFormat(AnumalCsv.class);
System.out.print(xmlDataFormat);
from(INBOX).unmarshal(xmlDataFormat).split(body()).process(new Processor() {
public void process(Exchange exchange) throws Exception {
String myString = exchange.getIn().getBody(String.class);
System.out.println("unmarshalled output" + myString);
}
}).marshal(csv).log("${body}").to("file:data/outbox?fileName=output1.csv").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String myString = exchange.getIn().getBody(String.class);
System.out.println("marshalled output" + myString);
System.out.print("test");
}
});
}
}
动物.java ~
import java.io.Serializable;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@CsvRecord(separator = ",", skipFirstLine = true)
public class Animal implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement
@DataField(pos = 1)
private String id;
@XmlElement
@DataField(pos = 2)
private String scientificName;
@XmlElement
@DataField(pos = 3)
private String status;
@XmlElement
@DataField(pos = 4)
private String veterinarian;
@XmlElement
@DataField(pos = 5)
private String created;
@XmlElement
@DataField(pos = 6)
private String age;
public Animal() {
}
public Animal(String id, String scientificName, String status, String veterinarian, String created, String age) {
this.id = id;
this.scientificName = scientificName;
this.status = status;
this.veterinarian = veterinarian;
this.created = created;
this.age = age;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public void setId(String id) {
this.id = id;
}
public void setCreated(String created) {
this.created = created;
}
public void setAge(String age) {
this.age = age;
}
public String getScientificName() {
return scientificName;
}
public void setScientificName(String scientificName) {
this.scientificName = scientificName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getVeterinarian() {
return veterinarian;
}
public void setVeterinarian(String veterinarian) {
this.veterinarian = veterinarian;
}
@Override
public String toString() {
return "Animal [id=" + id + ", scientificName=" + scientificName + ", status=" + status + ", veterinarian="
+ veterinarian + ", created=" + created + ", age=" + age + "]";
}
}
动物.java
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Animals implements Serializable{
private static final long serialVersionUID = 1L;
@XmlElement(name = "animal")
private List<Animal> animals;
public List<Animal> getAnimals() {
return animals;
}
public void setAnimals(List<Animal> animals) {
this.animals = animals;
}
}
AnimalCsv.java
package com.gcp.camel.animal;
import java.io.Serializable;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
@CsvRecord(separator = ",", skipFirstLine = true,generateHeaderColumns=true)
public class AnumalCsv {
@DataField(pos = 1,columnName="id" )
private String id;
@DataField(pos = 2,columnName="scientificName")
private String scientificName;
@DataField(pos = 3,columnName="status")
private String status;
@DataField(pos = 4,columnName="veterinarian")
private String veterinarian;
@DataField(pos = 5,columnName="created")
private String created;
@DataField(pos = 6,columnName="age")
private String age;
public AnumalCsv() {
super();
}
public AnumalCsv(String id, String scientificName, String status, String veterinarian, String created, String age) {
super();
this.id = id;
this.scientificName = scientificName;
this.status = status;
this.veterinarian = veterinarian;
this.created = created;
this.age = age;
}
public String getScientificName() {
return scientificName;
}
public void setScientificName(String scientificName) {
this.scientificName = scientificName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getVeterinarian() {
return veterinarian;
}
public void setVeterinarian(String veterinarian) {
this.veterinarian = veterinarian;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "AnumalCsv [id=" + id + ", scientificName=" + scientificName + ", status=" + status + ", veterinarian="
+ veterinarian + ", created=" + created + ", age=" + age + "]";
}
}
动物.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<animals>
<animal>
<id>Ziggy</id>
<scientificName>Spheniscus mendiculus</scientificName>
<status>bad</status>
<veterinarian>Ziggy</veterinarian>
<created>Ziggy</created>
<age>Ziggy</age>
</animal>
<animal>
<id>Ziggy</id>
<scientificName>Mustela nigripes</scientificName>
<status>good</status>
<veterinarian>Tiago</veterinarian>
<created>Ziggy</created>
<age>Ziggy</age>
</animal>
</animals>
获取输出:未编组输出:整个 xml 文件
编组的 outputid,scientificName,状态,兽医,创建,年龄