Link to home
Start Free TrialLog in
Avatar of no158
no158

asked on

Error: null values displaying

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;
import java.io.IOException;
import java.io.File;

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();
      }
     
      x.printStackTrace();
    } catch (SAXException sxe) {
      // Error generated during parsing)
      Exception x = sxe;
     
      if (sxe.getException() != null) {
        x = sxe.getException();
      }
     
      x.printStackTrace();
    } catch (ParserConfigurationException pce) {
      // Parser with specified options can't be built
      pce.printStackTrace();
    } catch (IOException ioe) {
      // I/O error
      ioe.printStackTrace();
    }
  }

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("  ");
    System.out.println(n.getLocalName());
  }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

listAll doesn't call println
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of no158
no158

ASKER

thanks again
:-)