Page 1 of 1

how can i create a CustomXmlBlock?

PostPosted: Tue Dec 27, 2016 6:29 pm
by eoikj
i try to create a CTCustomXmlBlock and append it to MainDocumentPart like P.but it said missing missing @XmlRootElement annotation during saving.
what's wring?

here are my code

Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      CTCustomXmlBlock ctCustomXmlBlock=new CTCustomXmlBlock();
      CTCustomXmlPr ctCustomXmlPr=new CTCustomXmlPr();
      ctCustomXmlBlock.getContent().add(ctCustomXmlPr);
      ctCustomXmlBlock.setElement("customizedElement");
      CTAttr ctAttr=new CTAttr();
      ctAttr.setName("testAttrName");
      ctAttr.setVal("testValue");
      ctAttr.setParent(ctCustomXmlPr);
      ctCustomXmlPr.getAttr().add(ctAttr);
      P p=new P();
      R r=new R();
      Text t=new Text();
      t.setValue("marked text here");
      r.getContent().add(t);
      p.getContent().add(r);
      ctCustomXmlBlock.getContent().add(p);
      wordMLPackage.getMainDocumentPart().getContent().add(ctCustomXmlBlock);
      wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_withCustomXml.docx") );

Re: how can i create a CustomXmlBlock?

PostPosted: Tue Dec 27, 2016 9:19 pm
by jason
ObjectFactory contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    @XmlElementDecl(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "customXml", scope = Body.class)
    public JAXBElement<CTCustomXmlBlock> createBodyCustomXml(CTCustomXmlBlock value) {
        return new JAXBElement<CTCustomXmlBlock>(_TrCustomXml_QNAME, CTCustomXmlBlock.class, Body.class, value);
    }
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


Use that to create JAXBElement<CTCustomXmlBlock>, and add this to wordMLPackage.getMainDocumentPart().getContent() instead.

Re: how can i create a CustomXmlBlock?

PostPosted: Thu Dec 29, 2016 5:32 pm
by eoikj
Hi jason thank you anyway.
yesterday my workmate told me customXmlBlock didn't support anymore since word2010,and i achived my idea by smartTag instead.

Re: how can i create a CustomXmlBlock?

PostPosted: Thu Dec 29, 2016 7:10 pm
by jason
Yes, I was curious how you were going to use it :-)

For anyone else ready, this is different from "CustomXML"; see https://blogs.technet.microsoft.com/gra ... t-on-word/