Sardor Dushamov
Sardor Dushamov

Reputation: 1667

how to parse .doc to .html for Android

i used apache poi lib but it parsed with style classnames. i need without class for android. Here's my code:

    HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(input);

    WordToHtmlConverter wordToHtmlConverter = null;
    try {
        wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        wordToHtmlConverter.processDocument(wordDocument);

        org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(out);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        out.close();

        String result = new String(out.toByteArray());
        System.err.println(result);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (TransformerException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }`

result:

<html><head>
        <style type="text/css">.b1{white-space-collapsing:preserve;}
    .b2{margin: 0.90555555in 0.66944444in 0.66944444in 0.66944444in;}
    .s1{text-transform:uppercase;color:black;}
    .s2{font-weight:bold;text-transform:uppercase;color:black;}
    .s3{color:black;}
    .s4{font-style:italic;color:black;}
    .s5{font-weight:bold;color:black;}
    .s6{font-weight:bold;font-style:italic;text-transform:uppercase;color:black;}
    .s7{font-weight:bold;color:maroon;}....</style>
</head>
<body>
<p class="p2"></p>
<p class="p2">
<span class="s2">...</span>
</p>
<p class="p2">
<br>
...</body>
</html>

it is suggested to parse without class names. I need androids' standard tags instead of classes.

Upvotes: 2

Views: 1595

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006934

Parse the resulting HTML and modify it to suit.

Upvotes: 2

Related Questions