1

I am trying to create a MS Word 2003 (.doc) file from an XML template. I am trying to replace my placeholders in the XML file with the content i want to insert in the word document. I have used the following code to replace the content:

Hashtable<String, String> ht = new Hashtable();
ht.put("NAME","XYZ");
ht.put("ROLL","123");       
while ((thisLine = reader.readLine()) != null) {
 if(thisLine.contains("##"))
 {
  for (java.util.Enumeration e = ht.keys(); e.hasMoreElements();) {
  String name = (String) e.nextElement();
  String value = ht.get(name).toString();
  thisLine = thisLine.replaceAll("##" + name.toUpperCase() + "##", value);
 }
}

##id## is my place holder in the XML file.

It runs properly when the placeholders are in a paragraph, but the problem with this code is that it does not replace the placeholders which are within a table in the XML word template.

So my question is how can I read the content of a table of an XML template using a simple java code so that I can replace the placeholders.

7
  • can you put your xml code? And see stackoverflow.com/questions/9884051/… Commented Mar 28, 2012 at 6:15
  • @Riddhish.Chaudhari: its not just a simple XML file, its actually a word file converted into XML. Commented Mar 28, 2012 at 6:25
  • if you have converted into xml, than it's have opening/closing tag so you can read it by DOM parser or using NodeList. Commented Mar 28, 2012 at 6:30
  • okay.. here it is <?mso-application progid="Word.Document"?> But the DOM parser will just allow me to view the contents in the file. Can I replace the contents using the code above?? Commented Mar 28, 2012 at 6:33
  • from getAttribute() you get tag attribute name,from getTagName() you get tag name ,and from getTextContent() you get the content inside tag. yes you replace the contents using above code. Commented Mar 28, 2012 at 6:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.