I am trying to get the current elementNode ( by means of the property "http://apache.org/xml/properties/dom/current-element-node" ) when the domparser reports an error, but I am getting a null node as result.
Using xerces 2.71 wiyh JAXP interface.
Can I use this property for this situation?



Program:
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class TestCurrentNode implements ErrorHandler
{

DocumentBuilderFactory dbf = null;

public Document parseDom(File xmlFile)
{

//create a SchemaFactory that conforms to W3C XML Schema

// get a DOM factory
dbf = DocumentBuilderFactory.newInstance();
// configure the factory
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(
"http://apache.org/xml/features/dom/defer-node-expansion",
Boolean.FALSE);

DocumentBuilder db;
try
{
db = dbf.newDocumentBuilder();
db.setErrorHandler(this);
System.out.println(this.getClass() + " parsetoDom");
return db.parse(xmlFile);
}
catch (ParserConfigurationException e)
{
// Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e)
{
// Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// Auto-generated catch block
e.printStackTrace();
}
return null;

}

public void warning(SAXParseException arg0) throws SAXException
{
System.out.println("instance warning " + arg0);

}

public void error(SAXParseException arg0) throws SAXException
{
System.out.println("instance error " + arg0);
Node node = null;

node =
(Node) dbf.getAttribute(
"http://apache.org/xml/properties/dom/current-element-node");

if (node != null)
{
System.out.println("node = " + node.getNodeName());
}
else
System.out.println("Node = null");
//e.printStackTrace();

//System.out.println("validator error " + arg0);
//System.out.println("cause " + arg0.getCause());

}

public void fatalError(SAXParseException arg0) throws SAXException
{
System.out.println("instance fatal " + arg0);

}
// DefaultHandler contain no-op implementations for all SAX events.
// This class should override methods to capture the events of interest.

public static void main(String[] args)
{
System.out.println("Progrma started");

File xmlFile = new File(args[0]);
if (!xmlFile.exists())
{
System.out.println("cannot find xml file " + args[0]);
System.exit(0);
}

TestCurrentNode testSax = new TestCurrentNode();
testSax.parseDom(xmlFile);
System.out.println("Program ended");

}
}


Input
<?xml version="1.0"?>
<!DOCTYPE notes [
<!ELEMENT note (heading,body,sub)>
<!ATTLIST note from CDATA #REQUIRED>
<!ATTLIST note to CDATA #REQUIRED>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT sub (#PCDATA)>
]>
<note from="Piet" to="Sint">
<heading>this is the heading</heading>
<bodsy>this is the body</bodsy>
</note>


Output
Progrma started
class TestCurrentNode parsetoDom
instance error org.xml.sax.SAXParseException: Document root element "note", must match DOCTYPE root "notes".
Node = null
instance error org.xml.sax.SAXParseException: Element type "bodsy" must be declared.
Node = null
instance error org.xml.sax.SAXParseException: The content of element type "note" must match "(heading,body,sub)".
Node = null
Program ended

Reply via email to