Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

DOM, NODE --> MyNode??

843834Apr 14 2005 — edited May 29 2005
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 26 2005
Added on Apr 14 2005
52 comments
531 views