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.Document Builder;
import javax.xml.parsers.Document BuilderFac tory;
import javax.xml.parsers.FactoryC onfigurati onError;
import javax.xml.parsers.ParserCo nfiguratio nException ;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseExcept ion;
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.new Instance() ;
//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 (ParserConfigurationExcept ion 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.getLo calName()) ;
}
}
Problem is that the values are showing up as null when displayed
import org.w3c.dom.*;
import javax.xml.parsers.Document
import javax.xml.parsers.Document
import javax.xml.parsers.FactoryC
import javax.xml.parsers.ParserCo
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseExcept
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.new
//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 (ParserConfigurationExcept
// 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.getLo
}
}
listAll doesn't call println
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks again
:-)