A
Andrew Neiderer
I am new to DOM programming.
I am simply trying to change an attribute value in an XML document.
The translation goes fine but I get an exception when running.
The code and input is small and is included below.
Please tell me what I am doing wrong with hasAttribute(). And maybe you can point me
to a URL and/or examples for Java processing of XML data for starters.
Thank you.
-------------------------------------------------------
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class BasicDOMapp {
// data for the class
protected static Document document;
protected static boolean debug = true;
// functions of the class
public static void parseXMLfile(String filename,boolean validating)
{
if ( debug )
System.out.println("\n parseXMLfile()\n");
try {
// create a builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validating);
// create the builder and parse the file
document = factory.newDocumentBuilder().parse(new File(filename));
return;
}
catch (SAXException e) {
// A parsing error occurred; the XML input is not valid.
}
catch (ParserConfigurationException e) {
}
catch (IOException e) {
}
return;
}
public static void main(String[] args) {
BasicDOMapp app = new BasicDOMapp();
app.parseXMLfile("note.xml",false);
Element element = document.getElementById("note");
boolean hasAttribute = element.hasAttribute("time");
// add an attribute
// element.removeAttribute("time");
}
}
----------- note.xml ------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<note time="12:03:46">
<to>
Tove
</to>
<from>
Jani
</from>
<heading>
Reminder
</heading>
<body>
Don't forget me this weekend!
</body>
</note>
--------------- typescript ----------------------------
Script started on Tue 27 Sep 2005 07:48:08 AM EDT
parseXMLfile()
Exception in thread "main" java.lang.NullPointerException
at BasicDOMapp.main(BasicDOMapp.java:51)
2 replan =>exit
Script done on Tue 27 Sep 2005 07:48:21 AM EDT
I am simply trying to change an attribute value in an XML document.
The translation goes fine but I get an exception when running.
The code and input is small and is included below.
Please tell me what I am doing wrong with hasAttribute(). And maybe you can point me
to a URL and/or examples for Java processing of XML data for starters.
Thank you.
-------------------------------------------------------
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class BasicDOMapp {
// data for the class
protected static Document document;
protected static boolean debug = true;
// functions of the class
public static void parseXMLfile(String filename,boolean validating)
{
if ( debug )
System.out.println("\n parseXMLfile()\n");
try {
// create a builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validating);
// create the builder and parse the file
document = factory.newDocumentBuilder().parse(new File(filename));
return;
}
catch (SAXException e) {
// A parsing error occurred; the XML input is not valid.
}
catch (ParserConfigurationException e) {
}
catch (IOException e) {
}
return;
}
public static void main(String[] args) {
BasicDOMapp app = new BasicDOMapp();
app.parseXMLfile("note.xml",false);
Element element = document.getElementById("note");
boolean hasAttribute = element.hasAttribute("time");
// add an attribute
// element.removeAttribute("time");
}
}
----------- note.xml ------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<note time="12:03:46">
<to>
Tove
</to>
<from>
Jani
</from>
<heading>
Reminder
</heading>
<body>
Don't forget me this weekend!
</body>
</note>
--------------- typescript ----------------------------
Script started on Tue 27 Sep 2005 07:48:08 AM EDT
parseXMLfile()
Exception in thread "main" java.lang.NullPointerException
at BasicDOMapp.main(BasicDOMapp.java:51)
2 replan =>exit
Script done on Tue 27 Sep 2005 07:48:21 AM EDT