Hi,
I need to do some implementation for a research project on XML. Basically I need to parse an XML document using DOM.
Then traverse the tree structure with the help of
NODE. I will take advantage of the comprehensive functions provided by interface NODE, e.g.
getAttributes(), getFirstChildNode()
...However, i still
need to accommodate some additional information, such as the path for each node,e.g.
Root.Child1.grandChild2.... To associate such extra data, I plan to create a subclass of
NODE,
MyNode, which may also implement
some methods, such as
setPath(), getPath()
...for extra usage of 'path' information.
My questions are:
1. Since
NODE is of type
Interface, is it possible to create such a subclass,
MyNode of it??
If yes, how should this be done? Since the tree structure is built by built-in function of DOM. Seems like no human intervention is allowed during the tree-building process.A typical piece of code using DOM may be:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("hello.xml");
Element root = document.getDocumentElement();
Node textNode = root.getFirstChild();
System.out.println(textNode.getNodeValue());
Blah Blah...
How should the above snippet be adapted to fit
MyNode? I think probably I should change the 4th line to:
MyNode root = builder.parse("hello.xml");
Will this work as intended?
2. How should the subclass,
MyNode be constructed??
What is in my mind is as follows:
public class MyNode extends Element
//or here maybe Node, this goes back to my first question;I am not exactly sure which should be the parent class) =|
{ String path = "";
...
//the constructor
public MyNode(Node originalNode){
super(); //should I invoke the constructor of its parent, NODE here???
this.setPath();
}
............
void setPath(){
this.path = (MyNode)(this.getParentNode()).getPath() + (MyNode)(this.getParentNode()).getNodeName();
}
String getPath(){
return this.path;
}
...........
}
Will the above construction of class
MyNode work properly??
Many thanks in advance for any help on this topic. Some code example is very appreciated.
Frank