Problem in getting parent node

When we try to parse document such as above with multiple line breaks as in the attached document.

While parsing this document paragraph by paragraph we get if the previous paragraph as a line break then i get node.getParentNode(); as null.

what is the work around for the same.

I am currently using aspose jar version 4.0.2

and planning it upgrade to 11.0

Hi Kumar,

Thank you for inquiry. Could you please share code snippet here. We will take a closer look into what’s happening and provide you with some further feedback.

Moreover,
please note that DocumentExplorer is a very useful tool which easily
enables us to see the entire document structure. You can find DocumentExplorer in
the folder where you installed Aspose.Words e.g. C:\Program Files
(x86)\Aspose\Aspose.Words for
.NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe.

Document objects are organised into a tree. See document object model for more information:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

I hope this will help.

Hi Imran,

when parsing 2 documents and adding contents from document 1 to document 2 using the following logic

public static void insertNodeInDocument(Node insertAfterNode, Node insertNode) throws Exception
{
    // Make sure that the node is either a paragraph or table.
    if (insertAfterNode.getNodeType() != NodeType.PARAGRAPH &&
        insertAfterNode.getNodeType() != NodeType.TABLE)
    {
        throw new Exception("The destination node should be either a paragraph or table.");
    }

    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.getParentNode();

    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(insertNode.getDocument(), insertAfterNode.getDocument(),
        ImportFormatMode.USE_DESTINATION_STYLES);

    Node newNode = importer.importNode(insertNode, true);

    if (dstStory != null)
    {
        dstStory.insertAfter(newNode, insertAfterNode);
    }
}

if my object “insertAfterNode” parent element is just a line break. It gives me null value at this line insertAfterNode.getParentNode();

Hi there,

Thanks for your inquiry.

Could you please attach the code you use to find the nodes where to insert the document at?

Thanks,

Hi,

I am using the following code :

private static void compareClauseText(DocumentBuilder odb, DocumentBuilder cdb, Node insertAfterClaNode, Node insertClaNode) throws Exception
{
    List oClauseTextParas = helper.getAllSubParas((Paragraph) insertAfterClaNode,
        ((Paragraph) insertAfterClaNode).getListFormat().getListLevelNumber());
    List cClauseTextParas = helper.getAllSubParas((Paragraph) insertClaNode,
        ((Paragraph) insertClaNode).getListFormat().getListLevelNumber());

    for (int i = 0; i <oClauseTextParas.size(); i++)
    {
        if (i <cClauseTextParas.size())
        {
            // Existing Clause Text
            Paragraph cClauseText = cClauseTextParas.get(i);
            Paragraph oClauseText = oClauseTextParas.get(i);

            if (!cClauseText.getText().equals(oClauseText.getText()))
            {
                odb.moveTo(oClauseText);
                cdb.moveTo(cClauseText);

                Node insertAfterClaTxtNode = odb.getCurrentParagraph();
                Node insertClaTxtNode = cdb.getCurrentParagraph();

                insertNodeInDocument(insertAfterClaTxtNode, insertClaTxtNode);
                insertAfterClaTxtNode.remove();
            }
        }
        else
        {
            // Deleted Clause Text
            Paragraph oClauseText = oClauseTextParas.get(i);
            odb.moveTo(oClauseText);
            Node insertAfterClaTxtNode = odb.getCurrentParagraph();
            insertAfterClaTxtNode.remove();
        }
    }

    for (int i = 0; i <cClauseTextParas.size(); i++)
    {
        if (oClauseTextParas.size() == 0)
        {
            Node insertAfterClaTxtNode = insertAfterClaNode; //.getNextSibling();
            Paragraph cClauseText = cClauseTextParas.get(i);
            cdb.moveTo(cClauseText);

            Node insertClaTxtNode = cdb.getCurrentParagraph();

            insertNodeInDocument(insertAfterClaTxtNode, insertClaTxtNode);
        }
        else if (i> oClauseTextParas.size())
        {
            // Added Clause Text
            Node insertAfterClaTxtNode = oClauseTextParas.size()> 0 ? oClauseTextParas
                .get(oClauseTextParas.size() - 1) : insertAfterClaNode;
            Paragraph cClauseText = cClauseTextParas.get(i);
            cdb.moveTo(cClauseText);

            Node insertClaTxtNode = cdb.getCurrentParagraph();

            insertNodeInDocument(insertAfterClaTxtNode, insertClaTxtNode);
        }
    }
}

odb is an original document,

cdb is an created document.

I am comparing to documents and trying to add the trying to incorporate the changes made in the created document to the original document.

while doing so if we have multiple line breaks as in the attached document, while parsing through them is my parent node of of my current paragraph is a new line character then i get node.get parentnode as null.

Thanks,

Kumar Sabnis

Software Engineer

Zycus Infotech Ltd.

Hi Kumar,

Thanks for your inquiry. The Aspose.Words library version you’re currently using is pretty old. There were some breaking changes behind the scenes to the API in between versions 4.0.2.0 and 11.2.0. Moreover, I am afraid, I can’t suggest you any work around for this issue. I would recommend you simply upgrade to the latest version of Aspose.Words. Please download it from the following link:
https://releases.aspose.com/words/java