ibrahimKiraz
ibrahimKiraz

Reputation: 411

Docx4j unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"p")

I am trying add html table in to the another word table' cell.

  1. I can add html table in to the another word table' cell. (OK)
  2. I can generated last word document call lastDocument.docx (OK)
  3. I can not load again WordprocessingMLPackage.load(lastDocument.docx), throw this exception Docx4j unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"p")

this is my code:

Tr workingRow = (Tr) XmlUtils.deepCopy(templateRow);
    List<?> textElements = WMLPackageUtils.getTargetElements(workingRow, Text.class);

    List<Tc> tcList = WMLPackageUtils.getTargetElements(workingRow, Tc.class);
    Tc tc = WMLPackageUtils.getTc(tcList, "${Replace_Tex1}");

    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    XHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
    XHTMLImporter.setTableFormatting(FormattingOption.IGNORE_CLASS);
    for (Object object : textElements) {
        Text text = (Text) object;
        if (!text.getValue().equals("${Replace_Tex1}"))
            continue;
        String replacementValue = (String) replacements.get(text.getValue());
        //text.setValue(replacementValue);
        R r = (R) text.getParent();
        r.getContent().clear();
        r.getContent().addAll(XHTMLImporter.convert(replacementValue, null));

Upvotes: 1

Views: 1190

Answers (2)

ibrahimKiraz
ibrahimKiraz

Reputation: 411

Hello i have fixed my code like that

     R r = (R) text.getParent();
     P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("");
     paragraphOfText.getContent().clear();

     r.getContent().clear();eviewtable.getContent().add(new Tr(new Tc(itemTable, new Paragraph())));
     tc.getContent().addAll(XHTMLImporter.convert(replacementValue, null));
     tc.getContent().add(paragraphOfText);

it is working :) Thank you @JasonPlutext

Upvotes: 0

JasonPlutext
JasonPlutext

Reputation: 15863

I guess your problem is:

  r.getContent().addAll(XHTMLImporter.convert(replacementValue, null));

Adding w:p (paragraph content) inside a run, which isn't allowed.

You can unzip your docx to look at word/document.xml

Upvotes: 1

Related Questions