node.getNextSibling() doesn't pick a specific node

while traversing document using node.getNextSibling() method from a node, it doesn’t pick some nodes. code snippet and doc file attached.

build environment - Java 1.8, aspose-words - 20.8

code snippet :

public void issue2(String wordFileLocation) {
        try {
            Document doc = new Document(wordFileLocation);
            NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
            Node node = null;
            for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {
                if (StringUtils.isNotEmpty(paragraph.getText()) && paragraph.getText().trim().equalsIgnoreCase("References")) {
                    node = paragraph;
                    break;
                }
            }
            while (null != node) {
                if (node.getText().trim().equalsIgnoreCase("System Life Cycle Procedures (SOPs & WPs)"))
                    System.out.println("-- found System Life Cycle Procedures (SOPs & WPs)");
                if (node.getText().trim().equalsIgnoreCase("Project References"))
                    System.out.println("-- found Project References");
                node = node.getNextSibling();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Doc file used : issue2_projectReference.docx (42.5 KB)

Expected Output:

-- found System Life Cycle Procedures (SOPs & WPs)
-- found Project References

Actual Output:

-- found System Life Cycle Procedures (SOPs & WPs)

@hkcw3 The output is correct, because “System Life Cycle Procedures (SOPs & WPs)” and “Project References” paragraphs are not siblings. These paragraphs are in different sections. Please see the following screenshot:


I think in your case, it would be easier to use DocumentVisitor to traverse nodes in your document.

Document doc = new Document("C:\\Temp\\in.docx");
doc.accept(new MyDocumentVisitor());
private static class MyDocumentVisitor extends DocumentVisitor
{
    @Override
    public int visitParagraphStart(Paragraph paragraph) throws Exception {

        if (paragraph.toString(SaveFormat.TEXT).trim().equalsIgnoreCase("System Life Cycle Procedures (SOPs & WPs)"))
            System.out.println("-- found System Life Cycle Procedures (SOPs & WPs)");
        if (paragraph.toString(SaveFormat.TEXT).trim().equalsIgnoreCase("Project References"))
            System.out.println("-- found Project References");

        return VisitorAction.CONTINUE;
    }
}