Re: How to learn to cope with Jrex - are there tutorials out there?
Benjy Cline <benjycline-/[email protected]> Wed, 31 Jan 2007 22:23:03 -0500
| Newsgroups | gmane.comp.mozilla.jrex |
|---|---|
| Message-ID | <[email protected]> |
Billy,
Here's what I did. It's not elegant, but it might
give you some idea. I did a lot of this through
experimentation, so it probably isn't the best way.
I have a class with three methods: doTree(), doElement(),
and doTagEnd(). doTree recursively walks the DOM. doElement()
is called when a tag is found. doTagEnd() is called
at the end of that tag. In this class, I merely print
beginning and ending tags and ignore any tag attributes. I
then override doElement() and doTagEnd() in a subclass
to do real work.
I get the document and call a recursive method called
doTree():
Document doc = navigation.getDocument() ;
Element ex = doc.getDocumentElement() ;
doTree((Node) ex) ;
doTree() looks like this:
public void doTree(Node node) {
if(node instanceof Element) {
Element element = (Element) node ;
// Visit tag.
doElement(element) ;
// Visit all the children, i.e., tags contained in this tag.
NodeList nl = element.getChildNodes() ;
if(nl == null) return ;
int num = nl.getLength() ;
for(int i=0; i<num; i++)
doTree(nl.item(i)) ;
// Process the end of this tag.
doTagEnd(element) ;
}
}
doElement() just prints the tag name:
public void doElement(Element element) {
System.out.println("<" + element.getTagName() + ">") ;
}
and doTagEnd() prints the closing tag:
public void doTagEnd(Element element) {
System.out.println("</" + element.getTagName() + ">") ;
}
To get tag attributes, I used Element.getAttributeNode().
I'm planning on posting a complete example on benjysbrain.com,
but who knows when that will happen?
I haven't looked at putting the browser inside an existing
frame. Perhaps someone else has an example.
Benjy
Billy wrote:
> Hi,
> thanks Benjy!
> That works great. But there are still some problems left:
> I got a JRexCanvas and a WebNavigation object. I know how to
> invoke webpages. Myproblem now is the DOM. I got the w3c.org
> Docoument by webnavi.getDocument(). What can I do with that
> docoment now? How to traverse it and extract the HTML-code?
> Additionally do you know how to invoke URIs inside a frame?
> I dont want to create frames, instead I want to invoke site
> inside of existing ones. Thank you again!
> Bestar regards
> Billy