tags:

views:

102

answers:

3

Dear all

  • I need to "proxy" a feed
  • and leave most of the nodes unspoiled
  • and transform the content of a few known nodes by calling some java methods
  • and return the transformed feed

Of course I prefer to avoid - loading in memory the whole feed - transform other nodes - bad performance

It's a kind of java pipe !

Thanks for your recommendations

A: 

Well I don't know about the "not loading into memory" but if you want to transform xml you should consider xslt and xpath.

Stevens
A: 

This is something I use:

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import org.w3c.dom.Document;
...
TransformerFactory mTransformFactory = TransformerFactory.newInstance();
cTransformer = mTransformFactory.newTransformer(new StreamSource(new StringReader(StringUtil.readFromResource("/foo.xslt"))));
Document mResultDoc = XmlUtil.createDocument();
Document mResultDoc = XmlUtil.parseXmlFile("foo.xml");
transformer.transform(new DOMSource(source), new DOMResult(mResultDoc));

Since you want to avoid memory overhead you should replace the DOMSource and DOMResult usage with SAX equivalents. The String and XML util class usage should be obvious from context.

Jherico
A: 

Thanks all for your answers : Here is the final result

TransformerFactory tFactory = TransformerFactory.newInstance();
InputStream xslt = FeedSecurityException.class.getResourceAsStream("/filter.xslt");
Transformer transformer = tFactory.newTransformer(new StreamSource(xslt));
transformer.transform(new StreamSource(input), new StreamResult(ouput));

And the XSLT which call the java method for a kind of node and leave the others untouched

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java"&gt;


<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />


<xsl:template match="CHANGED_NODE">
    <xsl:element name="CHANGED_NODE">
        <xsl:value-of select="java:com.mypackage.MyClass.tranformContent(.)"/>
   </xsl:element>
</xsl:template>

<xsl:template match="node()|@*">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>
CC
The transformContent is of course a static method of MyClass
CC