forked from binfalse/XMLUtils
/
NodeUsageExample.java
125 lines (110 loc) · 4.34 KB
/
NodeUsageExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
*
*/
package de.unirostock.sems.xmlutils.eg;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import de.unirostock.sems.xmlutils.ds.DocumentNode;
import de.unirostock.sems.xmlutils.ds.TreeDocument;
import de.unirostock.sems.xmlutils.ds.TreeNode;
import de.unirostock.sems.xmlutils.exception.XmlDocumentConsistencyException;
import de.unirostock.sems.xmlutils.exception.XmlDocumentParseException;
import de.unirostock.sems.xmlutils.tools.XmlTools;
/**
* @author Martin Scharm
*
*/
public class NodeUsageExample
{
/**
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws FileNotFoundException
* @throws XmlDocumentParseException
* @throws XmlDocumentConsistencyException
*/
public static void main (String[] args)
throws XmlDocumentParseException,
FileNotFoundException,
ParserConfigurationException,
SAXException,
IOException,
XmlDocumentConsistencyException
{
File file = new File ("test/simple.xml");
TreeDocument document = new TreeDocument (XmlTools.readDocument (file),
file.toURI ());
// get root node
DocumentNode root = document.getRoot ();
Vector<TreeNode> firstLevel = root.getChildren ();
System.out.println ("There are " + firstLevel.size () + " children in "
+ root.getXPath () + " :");
for (TreeNode kid : firstLevel)
System.out.println ("\t" + kid.getXPath () + " having "
+ ((DocumentNode) kid).getNumLeaves () + " leaves and a weight of "
+ kid.getWeight ());
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();
// get first message node
DocumentNode message = document.getNodeById ("messageone");
// you can also get access to the same node using it's path:
TreeNode sameNode = document.getNodeByPath (message.getXPath ());
// let's test if it's really the same:
System.out.println ("found same node by id and by XPath? "
+ (sameNode == message));
// you can also get this node by it's signature (here i know it's the first
// node having this hash value)
sameNode = document.getNodesByHash (message.getSubTreeHash ()).get (0);
// test:
System.out.println ("found same node by id and by hash? "
+ (sameNode == message));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();
// let's print some information about this node
System.out.println ("Path to the message node: " + message.getXPath ());
System.out.println ("Path to its parent: "
+ message.getParent ().getXPath ());
System.out.println ("Weight of the message node: " + message.getWeight ());
System.out.println ("Signature of the message node: "
+ message.getOwnHash ());
System.out.println ("Signature of the subtree rooted in the message node: "
+ message.getSubTreeHash ());
System.out.println ("#number nodes in its subtree: "
+ (message.getSizeSubtree () + 1));
System.out.println ("number of direct children: "
+ message.getNumChildren ());
System.out.println ("id of the node: " + message.getId ());
System.out.println ("tag name: " + message.getTagName ());
System.out.println ("attributes in this node:");
for (String attr : message.getAttributes ())
System.out.println ("\t" + attr + " => " + message.getAttribute (attr));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();
// remove the node from the tree
System.out.println ("# nodes in document before remove: "
+ document.getNumNodes ());
DocumentNode parent = message.getParent ();
parent.rmChild (message);
System.out.println ("# nodes in document after remove: "
+ document.getNumNodes ());
// and reinsert it
parent.addChild (message);
System.out.println ("# nodes in document after reinsert: "
+ document.getNumNodes ());
// note how the path ot the node has changed
System.out.println ("New path to the message node: " + message.getXPath ());
// but everything else is still the same
System.out.println ("Weight of the message node: " + message.getWeight ());
System.out.println ("Signature of the subtree rooted in the message node: "
+ message.getSubTreeHash ());
}
}