2015-04-21 67 views
0

我們有一個用例,我們需要從多個來源構建主xml。最初,我們將從服務中獲取xml,並使用此xml中的信息進行不同的數據庫調用以保存/獲取信息,最後構建主xml並保存到數據庫。我們使用帶有保險絲的駱駝。使用多個輸入構建xml

Here is our xml and camel routes. 

     <xml> 
      <xmlInformation> 
       ..... 
      </xmlInformation> 
      <customers> 
        <customer>...</customer> 
        <customer>...</customer> 
        <customer>...</customer> 
      </customers> 
      <products> 
       <product>....<product> 
       <product>....<product> 
       <product>....<product> 
       </products> 
     </xml> 

客戶和產品元素的數量是動態的,我們提取每一位客戶,產品從XML,保存到數據庫,並得到一些客戶,產品相關的ID和如下構建主XML。客戶分流的

<m:master> 
      <m:xmlInformation>....</m:xmlInformation> 
      <c:customers> 
       <c:customer id="12345">....</c:customer> 
       <c:customer id="22345">....</c:customer> 
       <c:customer id="32345">....</c:customer> 
      </c:customers> 
      <p:products> 
       <p:product id="22222">.....</p:product> 
       <p:product id="11111">.....</p:product> 
       <p:product id="33333">.....</p:product> 
      </p:products> 
    </m:master> 

Here is came route 

    <route id="routeA"> 
    <from uri="direct-vm:saveMasterXml" /> 
    <setProperty propertyName="originalIUPayload"><simple>${body}</simple></setProperty> 
    <splitter parallelProcessing=true stopOnException=true strategyRef="customersAggregator" > 
     <xpath>/xml/customers/customer</xpath> 
      <bean ref="customerService" method="saveCustomer" /> 
    </splitter> 
    <setProperty propertyName="customerXmls"><simple>${body}</simple></setProperty> 
    <setBody><simple>${property.originalIUPayload}</simple></setBody> 
<splitter parallelProcessing=true stopOnException=true strategyRef="productsAggregator" > 
     <xpath>/xml/products/product</xpath> 
      <bean ref="productService" method="getProductIds" /> 
    </splitter> 
    <setProperty propertyName="productIds"><simple>${body}</simple></setProperty> 
    <setBody><simple>${property.originalIUPayload}</simple></setBody> 
    <!-- transformation --> 
    <bean ref="masterService" method="saveMasterXml" /> 
    </route> 

輸出與IDS和產物分流器的輸出是產品ID列表豐富客戶的個XML列表。我可以使用xslt構建主xml,因爲大多數xml元素都是原始xml,但客戶和產品列表中的id需要傳遞給xslt。我被困在這裏解決它。歡迎任何建議。

回答

0

我使用.splitter(),然後.end(),然後再次得到原始消息。

由於您要保存到數據庫,因此您可能需要在分割之前使用.transacted()。請參閱「駱駝行動」一書,瞭解有關交易的更多信息。

,而不是和記錄人體的分裂的消息,你可以調用這個bean的客戶或產品

見例如:

import org.apache.camel.EndpointInject; 
import org.apache.camel.ProducerTemplate; 
import org.apache.camel.builder.RouteBuilder; 
import org.apache.camel.test.junit4.CamelTestSupport; 
import org.junit.Test; 

public class SplittingToXSLT extends CamelTestSupport { 

    @EndpointInject(uri = "direct:start") 
    private ProducerTemplate start; 

    @Test 
    public void testName() throws Exception { 
     context.addRoutes(new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       from("direct:start") 
        .split(xpath("/xml/customers/customer | /xml/products/product")) 
        .choice() 
        .when(xpath("/customer")) 
         .log("Customer ${body}") 
        .when(xpath("/product")) 
         .log("Product ${body}") 
        .end() 
        .log("${body}") 
       .to("xslt:test.xsl") 
       .log("${body}") 
       ; 
      } 
     }); 
     start.sendBody("<xml>\n" + 
      " <xmlInformation>\n" + 
      " </xmlInformation>\n" + 
      " <customers>\n" + 
      " <customer>1</customer>\n" + 
      " <customer>2</customer>\n" + 
      " </customers>\n" + 
      " <products>\n" + 
      " <product>3</product>\n" + 
      " <product>4</product>\n" + 
      " </products>\n" + 
      "</xml>"); 
     Thread.sleep(2000); 
    } 

} 

我不知道名字空間的XSLT作品,但這裏怎麼有一個例子無:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/"> 
    <master> 
     <xmlInformation> 
     <xsl:value-of select="/xml/xmlInformation"/> 
     </xmlInformation> 
     <customers> 
     <xsl:for-each select="/xml/customers/customer"> 
      <customer> 
      <xsl:attribute name="id"> 
       <xsl:value-of select="."/> 
      </xsl:attribute> 
      </customer> 
     </xsl:for-each> 
     </customers> 
     <products> 
     <xsl:for-each select="/xml/products/product"> 
      <product> 
      <xsl:attribute name="id"> 
       <xsl:value-of select="."/> 
      </xsl:attribute> 
      </product> 
     </xsl:for-each> 
     </products> 
    </master> 
    </xsl:template> 
</xsl:stylesheet>