Grails - Tika Content manipulation
<p>I'm trying to parse <code>.docx</code> file into <code>xml</code>. I can parse it and render the <code>xml</code> into a separate page. But what I really want is to display <code>&lt;body&gt;</code> only in a <code>template</code>, no <code>metadata</code>. How can I do this? I tried using <code>BodyContentHandler</code> but it got rid of <code>xml tags</code>.</p>

<p>Thank you.</p>

<p><strong>EDIT</strong></p>

<p>I had a simpler code in <code>controller</code> but I messed it up. This was what I had before. I'm getting the file from my <code>temp</code> folder and sending it to my <code>tikaService</code> (I copied the service from <a href="https://github.com/dewarim/tikaParser/blob/master/grails-app/services/tikaParser/TikaService.groovy" rel="nofollow noreferrer">GitHub</a>.)</p>

<p><code>Controller</code></p>

<pre><code>def parse(Document documentInstance) {
    def file = new File(documentInstance.fullPath)
    def result = tikaService.parseFile(file)
    render(view:"parse", text: result, contentType: "text/xml", encoding: "UTF-8")
}
</code></pre>

<p><code>Service</code></p>

<pre><code>class TikaService {

    static transactional = false

    String parseFile(File file, TikaConfig tikaConfig, Metadata metadata){
        SAXTransformerFactory factory = SAXTransformerFactory.newInstance()
        TransformerHandler handler = factory.newTransformerHandler()
        handler.transformer.setOutputProperty(OutputKeys.METHOD, "xml")
        handler.transformer.setOutputProperty(OutputKeys.INDENT, "yes")

        StringWriter sw = new StringWriter()
        handler.result = new StreamResult(sw)

        Parser parser = new AutoDetectParser(tikaConfig)
        ParseContext pc = new ParseContext()
        try {
            parser.parse(new FileInputStream(file), handler, metadata, pc)
            return sw.toString()
        } catch (Exception e) {
            log.error("Failed to parse file ${file.absolutePath}", e)
            throw e
        }
    }

    String parseFile(File file){
        TikaConfig tikaConfig = new TikaConfig()
        Metadata tikaMeta = new Metadata()
        return parseFile(file, tikaConfig, tikaMeta)
    }
}
</code></pre>

<p>If I use the <code>render</code> I get</p>

<p><a href="https://i.stack.imgur.com/HVJXV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HVJXV.png" alt="this"></a></p>

<p>When I call the result from <code>parse.gsp</code> with <code>${result}</code> I get</p>

<p><a href="https://i.stack.imgur.com/u3WaD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u3WaD.png" alt="this"></a></p>

<p>I hope I'm making sense explaining this. Thank you.</p>

<p><strong>EDIT 2</strong></p>

<p>XML</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta name="Revision-Number" content="0"/&gt;
&lt;meta name="Last-Printed" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="cp:revision" content="0"/&gt;
&lt;meta name="meta:print-date" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="meta:creation-date" content="2013-03-20T15:29:13Z"/&gt;
&lt;meta name="dcterms:modified" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="meta:save-date" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="dc:creator" content="ingo "/&gt;
&lt;meta name="Last-Modified" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="Author" content="ingo "/&gt;
&lt;meta name="dcterms:created" content="2013-03-20T15:29:13Z"/&gt;
&lt;meta name="date" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="X-Parsed-By" content="org.apache.tika.parser.DefaultParser"/&gt; 
&lt;meta name="X-Parsed-By" content="org.apache.tika.parser.microsoft.OfficeParser"/&gt;
&lt;meta name="modified" content="1601-01-01T00:00:00Z"/&gt;
&lt;meta name="creator" content="ingo "/&gt;
&lt;meta name="Creation-Date" content="2013-03-20T15:29:13Z"/&gt;
&lt;meta name="meta:author" content="ingo "/&gt;
&lt;meta name="Content-Type" content="application/msword"/&gt;
&lt;meta name="Last-Save-Date" content="1601-01-01T00:00:00Z"/&gt;
&lt;title/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p class="überschrift_1"&gt;&lt;b&gt;Tika Parser Test &lt;/b&gt;&lt;/p&gt;
&lt;p class="standard"&gt;This is a simple test document&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><strong>EDIT 3</strong></p>

<p>Controller</p>

<pre><code>import javax.xml.transform.OutputKeys
import javax.xml.transform.sax.SAXTransformerFactory
import javax.xml.transform.sax.TransformerHandler
import javax.xml.transform.stream.StreamResult

import org.apache.tika.config.TikaConfig
import org.apache.tika.metadata.Metadata
import org.apache.tika.parser.AutoDetectParser
import org.apache.tika.parser.ParseContext
import org.apache.tika.parser.Parser
import org.apache.tika.sax.BodyContentHandler
import org.apache.tika.sax.ToXMLContentHandler
import org.apache.tika.sax.ToHTMLContentHandler

    def parse(Document documentInstance) {
        def file = new File(documentInstance.fullPath)
        BodyContentHandler handler = new BodyContentHandler(new ToHTMLContentHandler())
        AutoDetectParser parser = new AutoDetectParser()
        FileInputStream inputstream = new FileInputStream(file)

        Metadata metadata = new Metadata()
        parser.parse(inputstream, handler, metadata)
    }
</code></pre>

<p>Error</p>

<p><code>Namespace http://www.w3.org/1999/xhtml not declared</code></p>