Hi Sven,

no, you can do it either way. I just proposed an option on how to accomplish 
the type conversion. Depending on how you want to process the XML it is of 
course also possible to directly remove the XML elements.

However here you can see a short example:

Route:

@Component
public class TranslateRoute extends SpringRouteBuilder {

  public static final String DIRECT_START_TRANSLATE = 
"direct://start-translate";
  public static final String LOG_TRANSLATE_ROUTE = 
"log://translate-route?level=INFO";

  @Override
  public void configure() {
    from(DIRECT_START_TRANSLATE)
        .to(LOG_TRANSLATE_ROUTE)
        .convertBodyTo(AggregateProduct.class)
        .to(LOG_TRANSLATE_ROUTE);
  }
}

Converter Using XPath Expressions:

@Converter
public class TranslateConverter {

  private static final Logger LOGGER = 
LoggerFactory.getLogger(TranslateConverter.class);

  @Converter
  public AggregateProduct convertInputStreamToAggregateProduct(InputStream 
inputStream) {

    final XPath xPath = XPathFactory.newInstance().newXPath();
    final Document document = createXMLDocument(inputStream);

    return createAggregateProduct(document);
  }

  private AggregateProduct createAggregateProduct(Document document) {
    final XPath xPath = XPathFactory.newInstance().newXPath();
    final AggregateProduct aggregateProduct = new AggregateProduct();
    try {
      aggregateProduct.setName(xPath.evaluate("/Product/Name", document));
      aggregateProduct.setDescription(
          xPath.evaluate("/Product/Size", document) + " "
          + xPath.evaluate("/Product/Weight", document));
      aggregateProduct.setPrice(Double.parseDouble(
          xPath.evaluate("/Product/Price", document)));
    } catch (XPathExpressionException ex) {
      LOGGER.error("Could not evaluate xPath Expression: '{}'", 
ex.getMessage());
    } catch (NumberFormatException ex) {
      LOGGER.error("Unable to parse String into Double: '{}'", ex.getMessage());
    }

    return aggregateProduct;
  }

  private Document createXMLDocument(InputStream inputStream) {

    try {
      final DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
      final DocumentBuilder builder = factory.newDocumentBuilder();
      final Document document = builder.parse(inputStream);
      return document;
    } catch (ParserConfigurationException ex) {
      LOGGER.error("Could not create the document builder: '{}'.", 
ex.getMessage());
    } catch (SAXException ex) {
      LOGGER.error("Unable to parse XML from InputStream: '{}'.", 
ex.getMessage());
    } catch (IOException ex) {
      LOGGER.error("Unable to read the InputStream: '{}'", ex.getMessage());
    }
    return null;
  }
}

Hope that helps!

- Christoph


On May 24, 2013, at 9:59 PM, Sven Richter wrote:

> Hi Christoph,
> 
> thank you for your answer. Looking at the documentation i only see
> examples for pojos. Does that mean i have to use pojos as data objects
> and cannot just use a convert function from a bean to remove the
> unneeded xml elements?
> 
> If you had a short example i would really appreciate this.
> 
> Best Regards,
> Sven
> 
> On Fri, May 24, 2013 at 4:47 PM, Christoph Emmersberger
> <[email protected]> wrote:
>> Hi Sven,
>> 
>> one option would be implementing a TypeConverter that takes the original 
>> message and transforms it into the output format.
>> 
>> Maybe this link might help you to resolve your transformation: 
>> http://camel.apache.org/type-converter.html
>> 
>> I've done similar things in the past by using some XPath queries that mapped 
>> onto the output format.
>> 
>> Hope that helps,
>> 
>> - Christoph
>> 
>> On May 24, 2013, at 2:56 PM, Sven Richter wrote:
>> 
>>> Hi everybody,
>>> 
>>> i have a general question about camel usage.
>>> I want to read an xml source, filter out a few tags, convert that to
>>> rss and put it into mongodb.
>>> What works is the reading of xml and storing rss into mongodb.
>>> 
>>> Now what i cannot get a clue of is how i convert that xml to rss AND
>>> filter out a few xml tags.
>>> I have found the camel-xmljson lib, but it looks like it only converts
>>> the whole xml w/o the possibility to filter.
>>> 
>>> How can i filter the xml? Do i have to convert it to a pojo and do it
>>> with plain code? Or is there a camel library available to do that?
>>> 
>>> Best Regards,
>>> Sven
>> 

Reply via email to