athanach
12-07-2007, 07:44 AM
I have a problem with the next code. I want to find the elements which are inside a tag. The code below it finds me the element inside the first tag it finds and not for all tags it has the page. For example in a rss feed may has a lot of news but if search for the tag <title> to get the titles of news it gives me only the first.
Any suggestions to make it work for all tags and not for the first only???
public void search(String filename) throws Exception {
File file = new File(filename);
DOMParser parser = new DOMParser();
parser.parse(file.toURL().toString());
Document doc = parser.getDocument();
// Get node to start iterating with
Element root = doc.getDocumentElement();
NodeList descriptionElements =
root.getElementsByTagName("title");
Element description = (Element)descriptionElements.item(0);
// Get a NodeIterator
NodeIterator i = ((DocumentTraversal)doc)
.createNodeIterator(description, NodeFilter.SHOW_ALL,
new FormattingNodeFilter(), true);
Node n;
while ((n = i.nextNode()) != null) {
String buf = n.getNodeValue();
System.out.println("Search phrase found: '" + buf + "'");
}
}
Any suggestions to make it work for all tags and not for the first only???
public void search(String filename) throws Exception {
File file = new File(filename);
DOMParser parser = new DOMParser();
parser.parse(file.toURL().toString());
Document doc = parser.getDocument();
// Get node to start iterating with
Element root = doc.getDocumentElement();
NodeList descriptionElements =
root.getElementsByTagName("title");
Element description = (Element)descriptionElements.item(0);
// Get a NodeIterator
NodeIterator i = ((DocumentTraversal)doc)
.createNodeIterator(description, NodeFilter.SHOW_ALL,
new FormattingNodeFilter(), true);
Node n;
while ((n = i.nextNode()) != null) {
String buf = n.getNodeValue();
System.out.println("Search phrase found: '" + buf + "'");
}
}