Page 1 of 1

Create docx from my Xml

PostPosted: Tue Oct 28, 2014 10:28 pm
by YannC
Hello, i am new in use of docx4j.
Since 2 or 3 days i am trying to undestrand how to create a docx file from my XML.

here my Xml :
Code: Select all
<body>
      <titre>MainTitre</titre>
      <texte niv="1" type="DMPART" id="1">
         <titre>titleniv1</titre>
         <p>myparagraphe</p>
         <p>otherPara</p>
         <liste type="PUCE">
            <item>firtsItem</item>
            <item>seconditem</item>
            <item>thirdItem</item>
         </liste>
         <p>paraAgain</p>
      </texte>
      <texte niv="1" type="DMPART" id="2">
         <titre>titleniv1</titre>
         <texte niv="2" type="puce" style="noir">
            <titre>titleNiv2</titre>
            <p>para </p>
         </texte>
         <texte niv="2" type="puce" style="noir">
            <titre>titleniv2</titre>
            <p>para</p>
            <p>paraAgain </p>
            <p>para</p>
         </texte>
      </texte>
   </body>


For my Docx , i would to have
style Title for /body/title
style title1 for texte[@niv='1']/titre
style title2 for texte[niv='2']/title

i would like to manage list item too.

Today i have my custom XML in the Docx as CustomXml/item1.xml (with sample "CreateDocxWithCustomXml"

But i dont understand how to make binding in order to have content texte displayed with style...

Any one to help me ?

Re: Create docx from my Xml

PostPosted: Wed Oct 29, 2014 12:11 am
by jason
For this, probably do not use custom XML data binding.

I'd suggest you use XSLT to transform your XML to w:document content, then inject it into your MainDocument part.

Or, you could explore using docx4j-ImportXHTML.

With the XSLT approach, the assumption is that your styles are suitably defined in the styles part.

With ImportXHTML, CSS can be used to style the XML. But there might be some assumptions in the code about element names/roles (eg table, list item, p), though of course you can modify it...