Page 1 of 1

replacing text .. why XML files?

PostPosted: Fri Mar 18, 2011 11:03 pm
by suman
Hi,
I am new to docx4j. Currently doing a POC for docx4j. It would be very good if some one could help me out. I ran through many sites but i am not able to understand why an xml file is being used instead of .java file. I need to replace the place holders from a template.docx file into another Sample.docx file. I am able to replace text but only some keys are replaced with values. Everything is the same but not sure what is happening. Also i need to replace a text in the header from the same template.docx file to the Sample.docx file. Can anyone help me on this. Please help me out with simple sample files and also please explain me why many people are using xml files.

thanks
suman

Re: replacing text .. why XML files?

PostPosted: Sun Mar 20, 2011 11:02 am
by jason
Hello Suman. I made your post into a topic in its own right. It didn't really belong in the 3 threads you posted it to.

suman wrote: I ran through many sites but i am not able to understand why an xml file is being used instead of .java file.


docx files unzipped are a collection of XML files. People sometimes post this XML content for discussion. docx4j, written iin java, creates/manipulates this XML.

suman wrote: I need to replace the place holders from a template.docx file into another Sample.docx file. I am able to replace text but only some keys are replaced with values. Everything is the same but not sure what is happening.


If you are doing string replacement, some of your keys might actually be split into separate runs for example key1: <w:r><w:t>ke</w:t></w:r><w:r><w:t>y1</w:t></w:r>

suman wrote: Also i need to replace a text in the header from the same template.docx file to the Sample.docx file. Can anyone help me on this.


You need to be able to work with the relevant header. Run the HeaderFooterList.java sample against your docx to get an idea how these are organised.

Re: replacing text .. why XML files?

PostPosted: Mon Mar 21, 2011 10:05 pm
by suman
Hi Jason,
Thanks for clearing my doubts. Really nice of u for helping me out. As i had already mentioned i need to read the header reference from the template and write the text from the template to another docx file. This is the code snippet already from the post which u had written to create the header. But i do not understand how to traverse back. ie read the header from the template and write it to another .docx file. Can you please help me in this.
I get the id of the header section. but how do i link the header section with the main document.

HeaderPart headerPart = new HeaderPart();
// headerPart.setContentType(new ContentType(
// "application/vnd.openxmlformats-package.relationships+xml"));
//
// headerPart
// .setRelationshipType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/header");
headerPart.setJaxbElement(getHdr());
Relationship relationship = wordprocessingMLPackage.getMainDocumentPart()
.addTargetPart(headerPart);

SectPr sectPr = objectFactory.createSectPr();

HeaderReference headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);/