Error: null values displaying

Posted on 2006-04-23
Last Modified: 2010-03-31
This program will take any xml file and show recursivly the depth of each tag
Problem is that the values are showing up as null when displayed

import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XMLDisplay {
   //Global variables
  private static int depth = 0;
  private static Document document;
  private static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   //Main method, calls recursive function
  public static void main(String[] args) {
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      document = builder.parse(new File(args[0]));
      listAll(document, depth);  
    } catch (SAXParseException spe) {
      // Error generated by the parser
      System.out.println("\n** Parsing error" + ", line " +
                         spe.getLineNumber() + ", uri " + spe.getSystemId());
      System.out.println("   " + spe.getMessage());
      // Use the contained exception, if any
      Exception x = spe;
      if (spe.getException() != null) {
        x = spe.getException();
    } catch (SAXException sxe) {
      // Error generated during parsing)
      Exception x = sxe;
      if (sxe.getException() != null) {
        x = sxe.getException();
    } catch (ParserConfigurationException pce) {
      // Parser with specified options can't be built
    } catch (IOException ioe) {
      // I/O error

public static void listAll(Node n, int depth)
    printName(n, depth);   // indent using level
    NodeList children = n.getChildNodes();
    for (int i=0; i < children.getLength(); i++) {
      Node child = children.item(i);
      listAll(child, depth+1);
  public static void printName(Node n, int depth) {
    for (int i = 0; i < depth; i ++)
      System.out.print("  ");
Question by:no158
    LVL 86

    Expert Comment

    listAll doesn't call println
    LVL 86

    Accepted Solution

    Sorry - ignore that. You need to check the node and do


    Author Comment

    thanks again
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now