Free Support Forum - aspose.com

Print XML of Node

The type Node has a function toString: https://apireference.aspose.com/java/words/com.aspose.words/node#toString(com.aspose.words.SaveOptions)

One of SaveOptions subclasses is OoxmlSaveOptions: https://apireference.aspose.com/java/words/com.aspose.words/OoxmlSaveOptions

One of the SaveFormats for OoxmlSaveOptions is FLAT_OPC: https://apireference.aspose.com/java/words/com.aspose.words/SaveFormat#FLAT_OPC

Here is the function I’m trying to write:

private void printDocument(com.aspose.words.Document aDoc) {
    Node n = aDoc.getFirstChild();

    SaveOptions sv = new OoxmlSaveOptions();
    sv.setSaveFormat(SaveFormat.FLAT_OPC);
    sv.setPrettyFormat(true);

    String nodeText = "";
    try {
        nodeText = n.toString(sv);
    } catch (Exception ex) {
        nodeText = ex.getMessage();
    }

    System.out.println(nodeText);
}

All that gets printed is “Exporting fragments of a document in this format is not supported.”

However, the decompiled code for the Node toString function looks like this:

public String toString(SaveOptions saveOptions) throws Exception {
    Object var10000;
    switch(saveOptions.getSaveFormat()) {
    case 50:
        var10000 = new zzZX0((HtmlSaveOptions)saveOptions);
        break;
    case 70:
        var10000 = new zzYTP((TxtSaveOptions)saveOptions);
        break;
    default:
        throw new IllegalStateException("Exporting fragments of a document in this format is not supported.");
    }

    return ((zzZRC)var10000).zzZt(this);
}

So, the documentation seems to be wrong. You can only print a Node via HTML or TXT. You seem to be unable to print an arbitrary Node’s XML contents.

This feature would be VERY useful for debugging purposes so that I can see what actual XML content Aspose is inserting into a document as it’s happening.

@noahbetzen,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-17539 . We will further look into the details of this requirement and will keep you updated on the status of the linked issue. We apologize for any inconvenience.

@noahbetzen,

Regarding WORDSNET-17539, it is to update you that we have completed the work on this issue and come to a conclusion that we will not be able to implement the fix to this issue. Your issue (WORDSNET-17539) will be closed with ‘Won’t Fix’ resolution.

There is no simple and even possible way to export standalone node to readable XML. For example, simple paragraph cannot exist without themes and styles. The other example - Chart node which has external XML, DML with its ID etc. We do not think that it makes sense to try implementing this feature.

If you are really interested to save separate node to FlatOpc, it can be done in the following way:

  • Clone the original document.
  • Remove all the nodes except node of interest.
  • Save to FlatOpc

@noahbetzen,

Regarding WORDSNET-17539, it is to update you that we have completed the analysis of this issue and come to a conclusion that we would not be able to implement the fix to this issue. Your issue (WORDSNET-17539) has now been closed with ‘Won’t Fix’ resolution. Please see my previous reply for details.