You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Sven Richter <sv...@googlemail.com> on 2013/05/24 14:56:27 UTC

Which components to use for xml -> rss -> mongodb workflow

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

Re: Which components to use for xml -> rss -> mongodb workflow

Posted by Sven Richter <sv...@googlemail.com>.
Thank you very much Christoph, i guess that will help me very much. At
least i have a good understanding of your approach now. Thank you for
your efforts and that nice example.

Best Regards,
Sven

On Sat, May 25, 2013 at 11:29 AM, Christoph Emmersberger
<ce...@googlemail.com> wrote:
> 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
>> <ce...@googlemail.com> 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
>>>
>

Re: Which components to use for xml -> rss -> mongodb workflow

Posted by Christoph Emmersberger <ce...@googlemail.com>.
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
> <ce...@googlemail.com> 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
>> 


Re: Which components to use for xml -> rss -> mongodb workflow

Posted by Sven Richter <sv...@googlemail.com>.
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
<ce...@googlemail.com> 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
>

Re: Which components to use for xml -> rss -> mongodb workflow

Posted by Christoph Emmersberger <ce...@googlemail.com>.
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


Re: Which components to use for xml -> rss -> mongodb workflow

Posted by Raul Kripalani <ra...@evosent.com>.
Hi Sven,

Simplest would be to apply an XSLT transformation to filter out unwanted
elements. With camel-saxon you get XSLT 2.0 support.

Use the identity transform along with rules that match the unwanted
elements and output nothing.

A Google search will bring up a lot of references of this popular XSLT
pattern.

Regards,
Raúl.
On 24 May 2013 13:56, "Sven Richter" <sv...@googlemail.com> 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
>