shadylookin
November 27th, 2009, 02:38 AM
I'm trying to parse an xmlString with java, but I'm having some problems
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class XMLTest {
public static void main(String args[]) {
String dummyString = "<?xml version=\"1.0\"?><test><possibleCheating numberOfTimesDeactive=\"3\" numberOfSecondsDeactive=\"15\" /></test>";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document answeredTest = db.parse(new InputSource(new StringReader(
dummyString)));
System.out.println(answeredTest.toString());
String numberOfDeactives = answeredTest.getElementById(
"possibleCheating").getAttribute("numberOfTimesDeactive");
String numberOfSecondsDeactive = answeredTest.getElementById(
"possibleCheating").getAttribute("numberOfSecondsDeactive");
System.out.println("student left the test " + numberOfDeactives
+ " times for a total of " + numberOfSecondsDeactive
+ " seconds");
} catch (Exception e) {
e.printStackTrace();
}
}
}
It outputs
[#document: null]
java.lang.NullPointerException
at XMLTest.main(XMLTest.java:23)
It looks like when I try and parse it nothing is happening then I get the null pointer. Anyone know what's wrong or an alternative way to do this?
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class XMLTest {
public static void main(String args[]) {
String dummyString = "<?xml version=\"1.0\"?><test><possibleCheating numberOfTimesDeactive=\"3\" numberOfSecondsDeactive=\"15\" /></test>";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document answeredTest = db.parse(new InputSource(new StringReader(
dummyString)));
System.out.println(answeredTest.toString());
String numberOfDeactives = answeredTest.getElementById(
"possibleCheating").getAttribute("numberOfTimesDeactive");
String numberOfSecondsDeactive = answeredTest.getElementById(
"possibleCheating").getAttribute("numberOfSecondsDeactive");
System.out.println("student left the test " + numberOfDeactives
+ " times for a total of " + numberOfSecondsDeactive
+ " seconds");
} catch (Exception e) {
e.printStackTrace();
}
}
}
It outputs
[#document: null]
java.lang.NullPointerException
at XMLTest.main(XMLTest.java:23)
It looks like when I try and parse it nothing is happening then I get the null pointer. Anyone know what's wrong or an alternative way to do this?