splitting an xml message and aggregating based on

2019-09-08 07:35发布

问题:

My inputXML:

 <Orders>
 <Order><OrderId>1</OrderId><Total>10</Total></Order>
 <Order><OrderId>2</OrderId><Total>20</Total></Order>
 <Order><OrderId>3</OrderId><Total>30</Total></Order>
 <Order><OrderId>4</OrderId><Total>40</Total></Order>
 <Order><OrderId>5</OrderId><Total>50</Total></Order>
<Order><OrderId>5</OrderId><Total>60</Total></Order>
<Order><OrderId>5</OrderId><Total>70</Total></Order>
<Order><OrderId>5</OrderId><Total>80</Total></Order>
<Order><OrderId>5</OrderId><Total>90</Total></Order>
 </Orders>

I need to read this input XML from a File. And need to write this to Different files based on the following conditions

  /Orders/Order/Total==10  then write this record to file1.
/Orders/Order/Total>10 and /Orders/Order/Total<=40 then write the records to file2.
/Orders/Order/Total>40 then write the records to file3.

my file 1 o/p(expected):

 <OrderId>1</OrderId><Total>10</Total>

my file 2 o/p(expected):

 <OrderId>2</OrderId><Total>20</Total>
 <OrderId>3</OrderId><Total>30</Total>
 <OrderId>4</OrderId><Total>40</Total>

my file 3 o/p(expected):

 <OrderId>5</OrderId><Total>50</Total>
 <OrderId>6</OrderId><Total>60</Total>
 <OrderId>7</OrderId><Total>70</Total>
 .
 .
 .

I am bit new to Mule ESB. I am confused with transformations and conversions of mule.

can some one suggest the best splitting and aggregating strategy and components to be used in my mule flow.Also the configuration to be used in components..

Please note that is a sample Input XML. I real time I need to process big XML files. So suggest the best solution. Thanks in advance!

回答1:

Use for each component and iterate to each Order element. Tip: use xpath. For example, put this on collection field #[xpath3('//*:Orders//*:Order', payload, 'NODESET')]

Inside your for each, add a choice component that has those conditions. Obviously, use xpath as well here. For instance, on when component #[Integer.parseInt(xpath3('.//Total',payload)) > 40]

On each choice's option, put a DOM to XML component to set the order element as payload, then add Object to String component. Last on the chain will be the file write/append logic which could be java transformer or expression component. (Better alternative is to use a StringBuilder where you just append each order then save the 3 files after the for each component).

Hope you know Java so the last part make sense.