Free Support Forum - aspose.com

Aspose.Words questions about XML binding in Words

Hi, I have created an XML to generate a PDF using Aspose.Pdf, and I’m wondering if it’s possible to use the same XML to generate, similarly, a doc. Does Aspose.Words support this functionality?

Hi Giorgos,

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behavior as MS Word do. You can only load file formats into Aspose.Words DOM listed in the following link:
http://www.aspose.com/docs/display/wordsjava/LoadFormat

Moreover, you can easily convert Pdf file to Doc file format. Please read following documentation link for your kind reference.
http://www.aspose.com/docs/display/pdfjava/Convert+PDF+file+into+DOC+format

Hope this answers your query. Please let us know if you have any more queries.

Thanks for the reply! Since I want the doc file to contain some hidden characters and I think PDF does not maintain such characters, is there a way to achieve the target?

Hi Giorgos,


Thanks for your inquiry. Please share your input XML file and manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Moreover, to be able to render hidden text to PDF, please try using the following code snippet:

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document(“in.docx”);<o:p></o:p>

for (Run r : (Iterable<Run>)doc.getChildNodes(NodeType.RUN, true))

r.getFont().setHidden(false);

doc.save("out.pdf");



Thanks for your reply.

My starting point is the XML, like the following example:

<?xml version="1.0" encoding="UTF-8"?>




Hello doc.





My desire would be to use this XML to generate the DOC like I do to generate the PDF. Is this possible?


Hi Giorgos,


Thanks for sharing the detail. First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
http://www.aspose.com/docs/display/wordsjava/Object+Model+Overview
http://www.aspose.com/docs/display/wordsjava/Composition+Diagrams

Secondly, In your case, you need to read XML nodes as shown below and write the its contents into Word document by using DocumentBuilder.write method. I suggest you, please read following documentation links for your kind reference.
http://www.aspose.com/docs/display/wordsjava/DocumentBuilder+Overview
http://www.aspose.com/docs/display/wordsjava/Using+DocumentBuilder+to+Modify+a+Document+Easily

Hope this helps you. Please let us know if you have any more queries.


<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;background:yellow;mso-highlight:
yellow;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;
font-family:“Courier New”;background:yellow;mso-highlight:yellow;mso-no-proof:
yes”> docAspose = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(docAspose);

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

javax.xml.parsers.DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

org.w3c.dom.Document doc = docBuilder.parse (new File(MyDir + "in.xml"));

// normalize text representation

doc.getDocumentElement ().normalize ();

org.w3c.dom.NodeList nodes = doc.getElementsByTagName("Text");

for(int s=0; s<nodes.getLength() ; s++){

org.w3c.dom.Node textnode = nodes.item(s);

if(textnode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){

org.w3c.dom.Element firstElement = (org.w3c.dom.Element)textnode;

org.w3c.dom.NodeList Segment = firstElement.getElementsByTagName("Segment");

org.w3c.dom.Element element = (org.w3c.dom.Element)Segment.item(0);

org.w3c.dom.NodeList textList = element.getChildNodes();

System.out.println(((org.w3c.dom.Node)textList.item(0)).getNodeValue().trim());

builder.writeln(((org.w3c.dom.Node)textList.item(0)).getNodeValue().trim());

}//end of if clause

}//end of for loop with s var

docAspose.save(MyDir + "out.docx");