apache caml将xml转换为json

时间:2017-11-20 17:17:43

标签: java apache-camel

我正在尝试使用apache camel xmltojson将xml转换为json,下面是我的两个类:

public class Route extends RouteBuilder {

@Override
public void configure() throws Exception {
    XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setForceTopLevelObject(true);
    from("file:resource/inbox").marshal(xmlJsonFormat).to("file:resource/outbox");
    }

 }

主要课程

 public class MainApp {

public static void main(String[] args) {
    Route routeBuilder = new Route();
    CamelContext context = new DefaultCamelContext();
    try {
        context.addRoutes(routeBuilder);
        context.start();
        Thread.sleep(5000);
        context.stop();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

以下是src / main / resource / inbox文件夹中的xml

   <message>
   <splash name="hello" text="hello"/>
   <splash name="Kaise ho" text="Kaise ho"/>
   <tag name="Latest" text="Latest"/>
   <tag name="New" text="New"/>
   <tag name="Just Added" text="Just Added"/>
   <tag name="Featured" text="Featured"/>
   <tag name="Popular" text="Popular"/>
   <tag name="Just Arrived" text="Just Arrived"/>
   <tag name="New Arrival" text="New Arrival"/>
   <promptlogin name="hello" text="hello"/>
   <promptsubscribe name="hello" text="hello"/>
   </message>

它给出了以下错误:

 org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> Marshal[org.apache.camel.model.dataformat.XmlJsonDataFormat@49070868] <<< in route: Route(route1)[[From[file:resource/inbox]] -> [Marshal[org.ap... because of Data format 'xmljson' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath

1 个答案:

答案 0 :(得分:0)

您是否已将camel-xmljson依赖项添加到项目中?

如果我没记错,您需要明确添加XOM依赖项(由于许可证不兼容):

<dependency>
  <groupId>xom</groupId>
  <artifactId>xom</artifactId>
  <version>1.2.5</version>
</dependency>