problem with getIterator(HTML.Tag)

P

Peter Wu

I am having problem in parsing the html documentation.

If my Tag = html.tag.a,
it has no problem to get it.

But if myTag = html.tag.input, it does not give me anything, although
the html documentation have both <a> & < input> tag..

Anyone know the reason?

Thanks

=================================================================



HTMLDocument.Iterator it = doc.getIterator(myTag);
while( it.isValid()){
SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();

System.out.println( s.toString() );

it.next();
}


java -version
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
 
C

Christian Kaufhold

Peter Wu said:
I am having problem in parsing the html documentation.

If my Tag = html.tag.a,
it has no problem to get it.

But if myTag = html.tag.input, it does not give me anything, although
the html documentation have both <a> & < input> tag..

Anyone know the reason?


HTMLDocument.Iterator only works for "inline" "tags", not for "block" "tags"
or "replaced" "tags" (IMG, INPUT, more?). Use this (nearly untested):

for (ReplacedIterator i = new ReplacedIterator((HTMLDocument)p.getDocument(), HTML.Tag.INPUT); i.hasNext();)
{
Element e = i.nextElement();

// ....
}





import javax.swing.text.html.HTMLDocument;
import javax.swing.text.Element;
import javax.swing.text.AttributeSet;

import java.util.Iterator;
import java.util.NoSuchElementException;



/** Replaced character elements, ordered by startOffset.
Modification of the Document text while iterating is forbidden.
*/
public final class ReplacedIterator
implements Iterator
{
private HTMLDocument document;

private Object name;

private Element element;


public ReplacedIterator(HTMLDocument d, Object name)
{
this.document = d;
this.name = name;

findNext(0);
}

private void findNext(int index)
{
while (index < document.getLength())
{
Element e = document.getCharacterElement(index);

if (name.equals(e.getAttributes().getAttribute(AttributeSet.NameAttribute)))
{
element = e;
return;
}

index = e.getEndOffset();
}

element = null;
}


public Object next()
{
return nextElement();
}

/** = next() */
public Element nextElement()
{
if (element == null)
throw new NoSuchElementException();

Element result = element;

findNext(result.getEndOffset());

return result;
}

public boolean hasNext()
{
return element != null;
}


public void remove()
{
throw new UnsupportedOperationException();
}
}





Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top