|
java.lang.Object
com.aspose.words.Node
- All Implemented Interfaces:
- java.lang.Cloneable, java.lang.Iterable
- Direct Known Subclasses:
- BookmarkEnd, BookmarkStart, CompositeNode, Inline
public abstract class Node - extends java.lang.Object
Base class for all nodes of a Word document.
A document is represented as a tree of nodes, similar to DOM or XmlDocument. For more info see the Composite design pattern. The Node class: - Defines the child node interface.
- Defines the interface for visiting nodes.
- Provides default cloning capability.
- Implements parent node and owner document mechanisms.
- Implements access to sibling nodes.
|
Method Summary |
abstract boolean | accept(DocumentVisitor visitor) | |
|
Accepts a visitor.
|
Node | deepClone(boolean isCloneChildren) | |
|
Creates a duplicate of the node.
|
Node | getAncestor(int ancestorType) | |
|
Gets the first ancestor of the specified NodeType.
|
Node | getAncestor(java.lang.Class ancestorType) | |
|
Gets the first ancestor of the specified object type.
|
java.lang.String | getText() | |
|
Gets the text of this node and of all its children.
|
java.util.Iterator | iterator() | |
| Provides support for the for each style iteration over child nodes of the node. |
Node | nextPreOrder(Node rootNode) | |
|
Gets next node according to the pre-order tree traversal algorithm.
|
Node | previousPreOrder(Node rootNode) | |
|
Gets the previous node according to the pre-order tree traversal algorithm.
|
void | remove() | |
|
Removes itself from the parent.
|
java.lang.String | toTxt() | |
|
Exports the content of the node into a string in plain text format.
|
|
Property Getters/Setters Detail |
getNodeType | |
public abstract int getNodeType()
|
-
Gets the type of this node.
The value of the property is NodeType integer constant.
Example: Shows how to enumerate immediate child nodes of a composite node using NextSibling. In this example we enumerate all paragraphs of a section body.
// Get the section that we want to work on.
Section section = doc.getSections().get(0);
Body body = section.getBody();
// Loop starting from the first child until we reach null.
for (Node node = body.getFirstChild(); node != null; node = node.getNextSibling())
{
// Output the types of the nodes that we come across.
System.out.println(node.getNodeType());
}Example: Shows how to remove all nodes of a specific type from a composite node. In this example we remove tables from a section body.
// Get the section that we want to work on.
Section section = doc.getSections().get(0);
Body body = section.getBody();
// Select the first child node in the body.
Node curNode = body.getFirstChild();
while (curNode != null)
{
// Save the pointer to the next sibling node because if the current
// node is removed from the parent in the next step, we will have
// no way of finding the next node to continue the loop.
Node nextNode = curNode.getNextSibling();
// A section body can contain Paragraph and Table nodes.
// If the node is a Table, remove it from the parent.
if (curNode.getNodeType() == NodeType.TABLE)
curNode.remove();
// Continue going through child nodes until null (no more siblings) is reached.
curNode = nextNode;
}
-
Gets the immediate parent of this node.
If a node has just been created and not yet added to the tree,
or if it has been removed from the tree, the parent is null. Example: Shows how to access the parent node.
// Create a new empty document. It has one section.
Document doc = new Document();
// The section is the first child node of the document.
Node section = doc.getFirstChild();
// The section's parent node is the document.
Assert.assertEquals(doc, section.getParentNode()); Example: Shows that when you create any node, it requires a document that will own the node.
// Open a file from disk.
Document doc = new Document();
// Creating a new node of any type requires a document passed into the constructor.
Paragraph para = new Paragraph(doc);
// The new paragraph node does not yet have a parent.
Assert.assertNull(para.getParentNode());
// But the paragraph node knows its document.
Assert.assertEquals(doc, para.getDocument());
// The fact that a node always belongs to a document allows us to access and modify
// properties that reference the document-wide data such as styles or lists.
para.getParagraphFormat().setStyleName("Heading 1");
// Now add the paragaph to the main text of the first section.
doc.getFirstSection().getBody().appendChild(para);
// The paragraph node is now a child of the Body node.
Assert.assertNotNull(para.getParentNode());
getDocument | |
public Document getDocument()
|
-
Gets the document to which this node belongs.
The node always belongs to a document even if it has just been created
and not yed added to the tree, or if it has been removed from the tree. Example: Shows that when you create any node, it requires a document that will own the node.
// Open a file from disk.
Document doc = new Document();
// Creating a new node of any type requires a document passed into the constructor.
Paragraph para = new Paragraph(doc);
// The new paragraph node does not yet have a parent.
Assert.assertNull(para.getParentNode());
// But the paragraph node knows its document.
Assert.assertEquals(doc, para.getDocument());
// The fact that a node always belongs to a document allows us to access and modify
// properties that reference the document-wide data such as styles or lists.
para.getParagraphFormat().setStyleName("Heading 1");
// Now add the paragaph to the main text of the first section.
doc.getFirstSection().getBody().appendChild(para);
// The paragraph node is now a child of the Body node.
Assert.assertNotNull(para.getParentNode());
getPreviousSibling | |
public Node getPreviousSibling()
|
-
Gets the node immediately preceding this node.
If there is no preceding node, a null is returned. Note: Calculating the value of this property iterates from the first child node of ParenNode
to this node. Example: Demonstrates use of methods of Node and CompositeNode to remove a section before the last section in the document.
// Document is a CompositeNode and LastChild returns the last child node in the Document node.
// Since the Document can contain only Section nodes, the last child is the last section.
Node lastSection = doc.getLastChild();
// Each node knows its next and previous sibling nodes.
// Previous sibling of a section is a section before the specified section.
// If the node is the first child, PreviousSibling will return null.
Node sectionBeforeLast = lastSection.getPreviousSibling();
if (sectionBeforeLast != null)
doc.removeChild(sectionBeforeLast);
getNextSibling | |
public Node getNextSibling()
|
-
Gets the node immediately following this node.
If there is no next node, a null is returned.
Example: Shows how to enumerate immediate child nodes of a composite node using NextSibling. In this example we enumerate all paragraphs of a section body.
// Get the section that we want to work on.
Section section = doc.getSections().get(0);
Body body = section.getBody();
// Loop starting from the first child until we reach null.
for (Node node = body.getFirstChild(); node != null; node = node.getNextSibling())
{
// Output the types of the nodes that we come across.
System.out.println(node.getNodeType());
}Example: Shows how to efficiently visit all direct and indirect children of a composite node.
public void RecurseAllNodes() throws Exception
{
// Open a document.
Document doc = new Document(getMyDir() + "Node.RecurseAllNodes.doc");
// Invoke the recursive function that will walk the tree.
TraverseAllNodes(doc);
}
/// <summary>
/// A simple function that will walk through all children of a specified node recursively
/// and print the type of each node to the screen.
/// </summary>
private void TraverseAllNodes(CompositeNode parentNode)
{
// This is the most efficient way to loop through immediate children of a node.
for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling())
{
// Do some useful work.
System.out.println(childNode.getNodeType());
// Recurse into the node if it is a composite node.
if (childNode.isComposite())
TraverseAllNodes((CompositeNode)childNode);
}
}
isComposite | |
public boolean isComposite()
|
-
Returns true if this node can contain other nodes.
This method returns false as Node cannot have child nodes.
Example: Shows how to efficiently visit all direct and indirect children of a composite node.
public void RecurseAllNodes() throws Exception
{
// Open a document.
Document doc = new Document(getMyDir() + "Node.RecurseAllNodes.doc");
// Invoke the recursive function that will walk the tree.
TraverseAllNodes(doc);
}
/// <summary>
/// A simple function that will walk through all children of a specified node recursively
/// and print the type of each node to the screen.
/// </summary>
private void TraverseAllNodes(CompositeNode parentNode)
{
/
|