Page 1 of 1

UmarshallFromTemplate doesn't work

PostPosted: Thu Oct 27, 2011 2:45 am
by MPav
Hello,

I need to replace some values from a template to a new docx-document. I have used the
example UnmarshallFromTemplate.class. But it didn't worked. The keys in the generated docx-document
weren't replaced. Only their braces were removed.
Of cource I changed the paths to template and new docx-document.

Is there any possibility for me to marshall the xml and don't use unmarshallFromTemplates?

thanks for answers

Sincerly
MPav

Re: UmarshallFromTemplate doesn't work

PostPosted: Thu Oct 27, 2011 9:26 am
by jason
Sounds like you are doing it wrong (possibly your key is split across text runs?) - have a look at the source docx Main Document Part xml to check.

As an alternative, I'd recommend you look at the OpenDoPE stuff.

Re: UmarshallFromTemplate doesn't work

PostPosted: Fri Oct 28, 2011 10:53 am
by lucasfgc
MPav,

I did exactly what u want to do and it does work.
Read the examples, try yourself and if you need help again... let me know, ok ?

Cheers

Re: UmarshallFromTemplate doesn't work

PostPosted: Wed Nov 02, 2011 9:51 pm
by MPav
Thanks for help,

i post here what I have written.

Code: Select all
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.JAXBContext;

import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Document;


public class UnmarshallFromTemplate {
   
   public static JAXBContext context = org.docx4j.jaxb.Context.jc;

   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {

      String inputfilepath = "C:\\Users\\Michael Pavlovski\\Desktop\\unmarshallFromTemplateExample.docx";
      
      boolean save = true;
      String outputfilepath = System.getProperty("user.dir") + "\\test-out.docx";      
      
      
   
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
      
      
      MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
      
         org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
               .getJaxbElement();
        
        
     
      
         String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);

        
        
         HashMap<String, String> mappings = new HashMap<String, String>();
        
         mappings.put("colour", "green");
         mappings.put("icecream", "chocolate");
        
        
        
         Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
        
    
         documentPart.setJaxbElement((Document) obj);

   
      if (save) {      
         SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
         saver.save(outputfilepath);
         System.out.println( "Saved output to:" + outputfilepath );
      } else {
         
         
      }
   }
   

}


I've used the same template which is used in the example UnmarshallFromTemplate.java.
In the log4j output I find this:

3690 [main] WARN org.docx4j.XmlUtils - Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>colour</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>' or key not mapped to a value
3690 [main] WARN org.docx4j.XmlUtils - Invalid key '</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>icecream</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidRPr="00912581"><w:rPr><w:lang w:val="en-US"/></w:rPr><w:t>' or key not mapped to a value

I don't understand why both keys can't be mapped.

Sincerly
MPav

Re: UmarshallFromTemplate doesn't work

PostPosted: Thu Nov 03, 2011 12:46 am
by jason
Again, it sounds like your key is split across text runs. If you don't understand what this means, please attach your docx and I will take a look.

Re: UmarshallFromTemplate doesn't work

PostPosted: Thu Nov 03, 2011 3:10 am
by MPav
I understand what this means, but the keys aren't split across of their textruns.
I used just the same template as the example uses. You can find it under [docx sources]\sample-docs\word\unmarshallFromTemplateExample.docx
The only thing i changed, was the path of the templatefile and the outputfile because they don't match my file system ;)

cheers

Re: UmarshallFromTemplate doesn't work

PostPosted: Thu Nov 03, 2011 9:24 am
by jason
I just ran the sample and confirmed it works for me.

Add System.out.println(xml) and tell us what it says?

Re: UmarshallFromTemplate doesn't work

PostPosted: Thu Dec 08, 2011 1:20 pm
by Paulo Martins
It worked well when I tested with the .docx provided from the site. But, if I change anything (for example, put a ";") and save, then it turns out to only repeat the key without {'s or }'s.

I 've saved the example with Work 2007. I'm using the version 2.7.1, and the original program example.

Considering that something is wrong after the update of the template .docx, I've uploaded the changed template.

Thanks for your help,

Paulo

Re: UmarshallFromTemplate doesn't work

PostPosted: Fri Dec 09, 2011 2:21 pm
by jason
If you look at the XML in your docx, you'll see:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
      <w:r w:rsidRPr="006243A9">
        <w:rPr>
          <w:lang w:val="en-US"/>
        </w:rPr>
        <w:t xml:space="preserve"> is ${</w:t>
      </w:r>
      <w:proofErr w:type="spellStart"/>
      <w:r w:rsidRPr="006243A9">
        <w:rPr>
          <w:lang w:val="en-US"/>
        </w:rPr>
        <w:t>colour</w:t>
      </w:r>
      <w:proofErr w:type="spellEnd"/>
      <w:r w:rsidRPr="006243A9">
        <w:rPr>
          <w:lang w:val="en-US"/>
        </w:rPr>
        <w:t>}.</w:t>
      </w:r>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Your key is split because of w:proofErr. To stop this happening, you should turn proofing off when creating your template.

Re: UmarshallFromTemplate doesn't work

PostPosted: Sat Dec 10, 2011 2:53 am
by Paulo Martins
It worked well.
Thanks a lot.

Paulo Martins

Re: UmarshallFromTemplate doesn't work

PostPosted: Tue Apr 16, 2013 10:33 pm
by jason