0

I want to convert multiple xml object to json object in apache camel. But I am just able to convert single object. With reference to below and attached code, I have tried some methods

  1. Make ArrayList in the Employee class
  2. ListJacksonDataFormat in XMLToJSONRoute class But neither work.

Could someone point out what I am doing wrong?

Employee.class

@DataField(pos = 1)
private int empId;
@DataField(pos = 2)
private String empName;
@DataField(pos = 3)
private int sal;

// XMLElement
public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

// XMLElement
public int getEmpId() {
    return empId;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

Main class

    XMLToJSONRoute routeBuilder = new XMLToJSONRoute();
    CamelContext ctx = new DefaultCamelContext();


    try {
        ctx.addRoutes(routeBuilder);
        ctx.start();
        Thread.sleep(5 * 60 * 1000);
        ctx.stop();

XMLtoJSON route

@Override
public void configure() throws Exception {

    //JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
    //Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();       


    JaxbDataFormat xmlDataFormat = new JaxbDataFormat();                        
    //1.  XML Data Format
    JAXBContext con = JAXBContext.newInstance(Employee.class);
    xmlDataFormat.setContext(con);



    JacksonDataFormat jsonDataFormat = new JacksonDataFormat(Employee.class); 
     // 1. JSON Data Format
    //ListJacksonDataFormat jsonDataFormat2 = new ListJacksonDataFormat(Employee.class); // 1. JSON Data Format

    from("file:C:/inputFolder/.camel/xml?noop=true")
    .unmarshal(xmlDataFormat)
                    //.marshal().xstream()
    //Unmarshaled debug part 
    .process(new Processor(){

    //Unmarshaled debug part 

    .marshal(jsonDataFormat)
    .to("file:C:/outputFolder/json").
    // Marshalled output 
                process(new Processor(){
                    public void process(Exchange exchange) throws Exception {

                ;}


}
Namphibian
  • 12,046
  • 7
  • 46
  • 76
Sam Li
  • 91
  • 1
  • 4
  • Possible duplicate of [Quickest way to convert XML to JSON in Java](https://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java) – Simion Aug 14 '18 at 17:03

1 Answers1

0

Looking at the source code of ListJacksonDataFormat, the reason might be that the list (of Employee beans) has to be an java.util.ArrayList:

public ListJacksonDataFormat() {
        useList();
    }

/**
 * Uses {@link java.util.ArrayList} when unmarshalling.
 */
public void useList() {
    setCollectionType(ArrayList.class);
} 
TacheDeChoco
  • 3,683
  • 1
  • 14
  • 17
  • I can solved it now. Actually there are two ways. Use ListJacksonDataFormat() and also use another class to wrap the employee class. – Sam Li Aug 16 '18 at 01:31