Hi Santosh,

you can use an Aggregator with a custom AggregationStrategy to build your output. One problem with the aggregator is that it needs to determine when to send the aggregated exchange. So in the example I used a timeout. This is not really good though. So if possible for your case the best is to simply send each city individually.

----
@XmlRootElement
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Cities {
    private List<City> city;

    public List<City> getCity() {
        if (city == null) {
            city = new ArrayList<City>();
        }
        return city;
    }
}
----

public class MyRouteBuilder extends RouteBuilder {

    public static void main(String... args) throws Exception {
        DefaultCamelContext context = new DefaultCamelContext();
        context.addRoutes(new MyRouteBuilder());
        context.setTracing(true);
        context.start();
        ProducerTemplate producer = context.createProducerTemplate();
        ArrayList<City> list = new ArrayList<City>();
        list.add(new City("Munich", "de"));
        list.add(new City("London", "en"));
        list.add(new City("Berlin", "de"));
        producer.sendBody("direct:start", list);
        Thread.sleep(3000);
        context.shutdown();
    }

    public void configure() {
AggregationStrategy aggregationStrategy = new AggregationStrategy() {

            @Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                if (oldExchange == null) {
                    oldExchange = newExchange.copy();
                    oldExchange.getIn().setBody(new Cities());
                }
Cities cities = (Cities) oldExchange.getIn().getBody(Cities.class); cities.getCity().add(newExchange.getIn().getBody(City.class));
                return oldExchange;
            }
        };
        from("direct:start").
            split(simple("body")).
            setHeader("countrycode", simple("${body.countrycode}")).
aggregate(header("countrycode"), aggregationStrategy).completionTimeout(1000). recipientList(simple("file:target/country-${header.countrycode}/?fileName=city.xml"));
    }
}


Am 23.11.2011 10:18, schrieb santoshjoshi:
Hi Christian,
My xml is like this, having cities as root element

<cities>
        <city>
                <citycode>XYZ</citycode>
                <countrycode>1</countrycode>
        </city>
        <city>
                <citycode>ABC</citycode>
                <countrycode>1</countrycode>
        </city>
        <city>
                <citycode>XYZ</citycode>
                <countrycode>2</countrycode>
        </city>
        <city>
                <citycode>ABC</citycode>
                <countrycode>2</countrycode>
        </city>
</cities>


and i want it to store it like
home
    --->country-ABC
             --->city.xml // with city 1 and 2 of country ABC

    --->country-XYZ
             --->city.xml // with city 1 and 2 of country XYZ


the appending of splited xml results in the below structure
*<?xml version="1.0" encoding="UTF-8" standalone="yes"?>*
<city>
     <citycode>XYZ</citycode>
     <countrycode>1</countrycode>
</city>
*<?xml version="1.0" encoding="UTF-8" standalone="yes"?>*
<city>
     <citycode>XYZ</citycode>
     <countrycode>2</countrycode>
</city>


--
View this message in context: 
http://camel.465427.n5.nabble.com/How-to-write-or-append-content-to-XML-file-without-XML-declaration-tp5014969p5016045.html
Sent from the Camel - Users mailing list archive at Nabble.com.


--
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com

Reply via email to