Listing 11.2 searcher.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
public class searcher
{
public static void main(String args[])
{
FindElements
findElements = new FindElements(args[0], args[1]);
}
}
class FindElements
{
static String displayStrings[] = new
String[1000];
static int numberDisplayLines = 0;
static String searchFor;
public FindElements (String uri, String
searchString)
{
searchFor =
searchString;
displayDocument(uri);
for(int loopIndex = 0;
loopIndex < numberDisplayLines; loopIndex++){
System.out.println(displayStrings[loopIndex]);
}
}
public static void displayDocument(String
uri)
{
try {
DOMParser parser = new DOMParser();
parser.parse(uri);
Document document =
parser.getDocument();
NodeList nodeList = document.getElementsByTagName(searchFor);
if (nodeList != null) {
for (int loopIndex = 0; loopIndex < nodeList.getLength();
loopIndex++ ) {
display(nodeList.item(loopIndex), "");
}
}
} catch (Exception e)
{
e.printStackTrace(System.err);
}
}
public static void display(Node node, String
indent)
{
if (node == null) {
return;
}
int type =
node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
displayStrings[numberDisplayLines] = indent;
displayStrings[numberDisplayLines] +=
"<?xml version=\"1.0\" encoding=\""+
"UTF-8" + "\"?>";
numberDisplayLines++;
display(((Document)node).getDocumentElement(), "");
break;
}
case Node.ELEMENT_NODE: {
displayStrings[numberDisplayLines] = indent;
displayStrings[numberDisplayLines] += "<";
displayStrings[numberDisplayLines] += node.getNodeName();
int
length = (node.getAttributes() != null) ?
node.getAttributes().getLength() : 0;
Attr attrs[] = new Attr[length];
for (int loopIndex = 0; loopIndex < length; loopIndex++) {
attrs[loopIndex] =
(Attr)node.getAttributes().item(loopIndex);
}
for (int loopIndex = 0; loopIndex < attrs.length;
loopIndex++) {
Attr attr = attrs[loopIndex];
displayStrings[numberDisplayLines] += " ";
displayStrings[numberDisplayLines] += attr.getNodeName();
displayStrings[numberDisplayLines] += "=\"";
displayStrings[numberDisplayLines] +=
attr.getNodeValue();
displayStrings[numberDisplayLines] += "\"";
}
displayStrings[numberDisplayLines] += ">";
numberDisplayLines++;
NodeList childNodes = node.getChildNodes();
if (childNodes != null) {
length = childNodes.getLength();
indent += " ";
for (int loopIndex = 0; loopIndex < length; loopIndex++ ) {
display(childNodes.item(loopIndex), indent);
}
}
break;
}
case Node.CDATA_SECTION_NODE: {
displayStrings[numberDisplayLines] = indent;
displayStrings[numberDisplayLines] += "<![CDATA[";
displayStrings[numberDisplayLines] += node.getNodeValue();
displayStrings[numberDisplayLines] += "]]>";
numberDisplayLines++;
break;
}
case Node.TEXT_NODE: {
displayStrings[numberDisplayLines] = indent;
String newText = node.getNodeValue().trim();
if(newText.indexOf("\n") < 0 && newText.length() > 0)
{
displayStrings[numberDisplayLines] += newText;
numberDisplayLines++;
}
break;
}
case Node.PROCESSING_INSTRUCTION_NODE: {
displayStrings[numberDisplayLines]
= indent;
displayStrings[numberDisplayLines] += "<?";
displayStrings[numberDisplayLines] += node.getNodeName();
String text = node.getNodeValue();
if (text != null
&& text.length() > 0) {
displayStrings[numberDisplayLines] += text;
}
displayStrings[numberDisplayLines] += "?>";
numberDisplayLines++;
break;
}
}
if (type ==
Node.ELEMENT_NODE) {
displayStrings[numberDisplayLines] = indent.substring(0,
indent.length() - 4);
displayStrings[numberDisplayLines] += "</";
displayStrings[numberDisplayLines] += node.getNodeName();
displayStrings[numberDisplayLines] += ">";
numberDisplayLines++;
indent+= " ";
}
}
}
The examples we've created so far have all created text-based
output using the System.out.println method. However, few browsers
these days work that way. In the next section, I'll take a look at
creating a windowed browser.