我正在使用docx4j將html字符串轉換爲docx。NullPointer使用docx4j將HTML字符串轉換爲Docx時出現異常
以下是代碼。
package docx4j;
import org.docx4j.convert.in.xhtml.FormattingOption;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class ConvertHTMLToDoc {
public static void main(String[] args) throws Docx4JException {
String outputfilepath = "style-example-OUT30.rtf";
String text = getHTMLString();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporter xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
/* xHTMLImporter.setParagraphFormatting(FormattingOption.CLASS_PLUS_OTHER);
xHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);
*/
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));
/* wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Testing Title");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle", "Testing Subtitle");
*/
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
private static String getHTMLString() {
String text = "<html><head><title></title></head><body>" + "<p class=\"Title\">Testing Title</p>"
+ "<p class=\"Subtitle\">Testing Subtitle</p>" + "</body></html>";
return text;
}
}
當我運行這個,我得到在下面的行錯誤。
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));
以下是錯誤信息。
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) java.lang.NullPointerException
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.jaxb.NamespacePrefixMapperUtils.setProperty(NamespacePrefixMapperUtils.java:155)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:588)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:559)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart.createVirtualStylesForDocDefaults(StyleDefinitionsPart.java:369)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:172)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:161)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.stylesToCSS(XHTMLImporterImpl.java:446)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getRenderer(XHTMLImporterImpl.java:253)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:645)
什麼可能是空指針異常的原因?
我剛纔運行你的代碼,它工作正常。你是否也有這種依賴性:'docx4j-ImportXHTML'? –
當我作爲獨立運行它時,它工作正常。當我運行這是JBOSS AS 6.2中的Web應用程序的一部分時,它不起作用。 – yetanothercoder
我沒有安裝jboss 6.2,但使用tomcat 8進行了檢查。它正在工作。您確定您擁有從html轉換爲docx所需的所有庫嗎?你如何構建包含所有必需庫的包?你是否正在使用maven和包裝戰爭進行建築? –