2017-11-29 10 views
1

apache camelを使用してxmlをjsonに変換しようとしていますが、うまくいきます。しかし、アウトにはすべての属性に「@」が付いています。apache camelを使用してxmlから変換する際に '@'記号が出力jsonに入力される

例:xmlがAAAの場合、jsonで@name:ajayを取得しています。

<dependency> 
    <groupId>org.json</groupId> 
    <artifactId>json</artifactId> 
    <version>${json.version}</version> 
</dependency> 

そして、このようなProcessor実装:

以下

iは、代替ライブラリーとして

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat(); 
    CamelContext context = new DefaultCamelContext(); 
    try { 
     xmlJsonFormat.setEncoding("UTF-8"); 

     xmlJsonFormat.setForceTopLevelObject(true); 
     xmlJsonFormat.setTrimSpaces(true); 
     xmlJsonFormat.setSkipNamespaces(true); 
     xmlJsonFormat.setRemoveNamespacePrefixes(true); 

     context.addRoutes(new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       String ch = "" ; 
    from("file:src/main/resources/input").marshal(xmlJsonFormat).to("file:src/main/resources/output/"); 

     System.out.println(ch); 
      } 
     }); 

     context.start(); 

     Thread.sleep(10000); 
    } finally { 
     context.stop(); 
    } 
+0

xmljsonサードパーティのライブラリは、デフォルトでそれをしない方法です。可能であれば、そうしないようにこのライブラリを構成する方法を調べる必要があります。そして、私はこのライブラリが死んでいる/死んでいると思うし、私たちはcamel-xmljsonコンポーネントも非難しました。 –

+0

他の方法で私はjsonにxmlを変換できますか? –

+0

@HimanshuKhandelwal、これを適切な方法で変換する方法について考える必要があります。ジャクソンライブラリーを変換用に使用することはできますが、それはすべてjsonフォーマットの仕方に依存します。 –

答えて

0

を使用していたコードである、あなたはJSON.orgを使用することができ

public void configure() throws Exception { 
    from("direct:input") 
     .convertBodyTo(String.class) 
     .process(new Processor() { 
      public void process(Exchange exchange) throws Exception { 
       final String xmlBody = exchange.getIn().getBody(String.class); 
       final String jsonBody = XML.toJSONObject(xmlBody).toString(); 
       exchange.getIn().setBody(jsonBody); 
      } 
     }) 
     .log("************* My body in json format is ${body} *********") 
     .to("mock:output");     
} 

JUnitテスト:

@Test 
public void test() throws InterruptedException, CamelExecutionException, IOException { 
    getMockEndpoint("mock:output").expectedBodyReceived().body().contains("{\"books\""); 
    getMockEndpoint("mock:output").expectedMessageCount(1); 


    template.sendBody("direct:input", 
        IOUtils.toString(this.getClass().getResourceAsStream("/xml/books.xml"), 
            Charset.defaultCharset())); 

    getMockEndpoint("mock:output").assertIsSatisfied(); 
} 

この入力を考える:

<books> 
    <book> 
     <title>The Fellowship of the Ring</title> 
     <author>J.R.R Tolkien</author> 
     <year>1954</year> 
    </book> 
    <book> 
     <title>The Two Towers</title> 
     <author>J.R.R Tolkien</author> 
     <year>1955</year> 
    </book> 
    <book> 
     <title>The Return of the King</title> 
     <author>J.R.R Tolkien</author> 
     <year>1956</year> 
    </book> 
</books> 

私たちは、この出力があります。

{ 
"books":{ 
    "book":[ 
     { 
      "year":1954, 
      "author":"J.R.R Tolkien", 
      "title":"The Fellowship of the Ring" 
     }, 
     { 
      "year":1955, 
      "author":"J.R.R Tolkien", 
      "title":"The Two Towers" 
     }, 
     { 
      "year":1956, 
      "author":"J.R.R Tolkien", 
      "title":"The Return of the King" 
     } 
    ] 
} 
} 
関連する問題