using DOM for XML processing

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
 
A

Andrew Thompson

Andrew Neiderer wrote:


....
app.parseXMLfile("note.xml",false);

Element element = document.getElementById("note");

System.out.println( "element: " + element ); //check it!
boolean hasAttribute = element.hasAttribute("time");
....
 
C

carlosrf82

Opps
Element element = document.getElementById("note");
Return a null point because there is not an element with id="note" in
the document.
I think, you want an instruction like this:
Element element = document.getElementByTagName("note");
To get a element by id , you need create a DTD that declare a id
attribute for some element, then use it. And also setValiditing() need
be "true"
 
A

Andrew Thompson

'note' is the name of an element, by the way, it is
not an 'id'. An id has the form,

<to id='note'>

Perhaps what you require is Document.getElementsByTagName()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top