2017-09-15 57 views
0

我想打印到控制檯與Apache駱駝格式化XML文件:XML文件標準:出(格式化)與Apache駱駝

預期成果是:

XML - >

<client book="Camel in Action" author="4995" title="1"/> 

STD:出 - >

Client 
book: Camel in Action 
author: 4995 
title: 1 

我當前的代碼:

public class MyRouteBuilder extends RouteBuilder { 
     @Override 
     public void configure() throws Exception { 

     JAXBContext jaxbContext =  
     JAXBContext.newInstance(Client.class); 
     JaxbDataFormat jaxbDataFormat = new 
     JaxbDataFormat(jaxbContext); 


     from("file:/home/tkaczmarek/usr/data/inbox/") 
      .unmarshal(jaxbDataFormat) 
      .to("file:/home/tkaczmarek/usr/data/outbox") 
      .log("${body}"); 
    } 
} 

Client.java

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Client { 

@XmlAttribute 
private String book; 

@XmlAttribute 
private String author; 

@XmlAttribute 
private String title; 
} 

如何,我可以得到預期的輸出?現在它只是打印到標準:只是xml文件與標籤:(

+0

如果刪除解組(jaxbDataFormat)? – ltsallas

+0

不工作,與以前相同的輸出刪除 – Tomasz

+0

在解組和之間放置.to(「log ...」)?(「file ...」)? – user3206236

回答

0

好吧,所以我不完全明白你在幹什麼,但是,而不是log(),你可以使用bean()如下所示打印在任何你想要的方式解組 XML和格式化。

from("file:/home/tkaczmarek/usr/data/inbox/") 
    .to("file:/home/tkaczmarek/usr/data/outbox") 
    .bean(XmlToBean.class); 

這裏是XmlToBean講座

public class XmlToBean { 
    public void transformXmlObject(Client client){ 
     System.out.println(client.toString()); 
    } 
} 

注意,我還沒有使用unmarshal(),因爲只有在classpath中才需要它,除非你有一些特定的要求。如果你想添加另一個類是矯枉過正,一個process()還就足夠了,如圖如下─

from("file:/home/tkaczmarek/usr/data/inbox/") 
    .to("file:/home/tkaczmarek/usr/data/outbox") 
    .process(exchange -> { 
     System.out.println(exchange.getIn().getBody(Client.class)); 
    }); 
+0

非常感謝你! – Tomasz