As you see in that figure, the program works as it should-the
document appears with all elements and text intact, indented
properly. Congratulations-now you're able to handle most of what
you'll find in XML documents using the XML for Java packages. The
complete listing for IndentingParser.java is in Listing 11.1. Note
that you can use this program as a text-based browser: You can give
it the name of any XML document on the Internet-not just local
documents-to parse, and it'll fetch that document and parse it.
Listing 11.1 IndentingParser.java
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
public class IndentingParser
{
static String displayStrings[] = new
String[1000];
static int numberDisplayLines = 0;
public static void displayDocument(String
uri)
{
try {
DOMParser parser = new DOMParser();
parser.parse(uri);
Document document = parser.getDocument();
display(document, "");
} 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 attributes[] = new Attr[length];
for (int loopIndex = 0; loopIndex < length; loopIndex++) {
attributes[loopIndex] =
(Attr)node.getAttributes().item(loopIndex);
}
for (int loopIndex = 0; loopIndex < attributes.length;
loopIndex++) {
Attr attribute = attributes[loopIndex];
displayStrings[numberDisplayLines] += " ";
displayStrings[numberDisplayLines] +=
attribute.getNodeName();
displayStrings[numberDisplayLines] += "=\"";
displayStrings[numberDisplayLines]
+=
attribute.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 += " ";
}
}
public static void main(String args[])
{
displayDocument(args[0]);
for(int loopIndex = 0;
loopIndex < numberDisplayLines; loopIndex++){
System.out.println(displayStrings[loopIndex]);
}
}
}