[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSAX DocumentBuilder.parse throws "stealth" XMConfigurationException

Posted on 2009-04-22
26
Medium Priority
?
839 Views
Last Modified: 2013-11-23
I have the code below, which is suppose to take a file and return a DOM parsed Document. When I instantiate the class the init method doesn't work, and doesn't throw and exception either. (Doesn't work, IE drDoc and drRoot return null)
Upon debug I noticed that when parse(filename)  is called, the debugger registers a XMLConfigurationException with arg0=http://apache.org/xml/features/standard-uri-conformant, but there is no cause of stacktrace (both null) and the exception doesn't get caught by anything I wrote. Instead it seems that the Java code itself catches the exception and just returns null.
The XML file looks as follows:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE serverConfig [
<!ELEMENT serverConfig (comm)>
<!ELEMENT comm (listenPort,timeoutSec)>
<!ELEMENT listenPort (#PCDATA)>
<!ELEMENT timeoutSec (#PCDATA)>
]>

<serverConfig>
      <comm>
            <listenPort>33333</listenPort>
            <timeoutSec>90</timeoutSec>      
      </comm>
</serverConfig>

Any ideas?
Thanks.
package xml;
 
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.Element;
import org.xml.sax.SAXException;
 
public class XMLDocumentResolver{
	private DocumentBuilder docBuilder=null;
	private Document drDoc=null;
	private Element drRoot=null;
 
	public void initXMLDocumentResolver(File filename){
		try {
			System.out.println(filename.getCanonicalPath()+"\n");
			System.out.println(filename.exists());
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(true);
            dbf.setIgnoringElementContentWhitespace(true);
       
            docBuilder = dbf.newDocumentBuilder();
 
        	DocumentBuilder db = getDocumentBuilder();
            drDoc = db.parse(filename);
            drRoot = drDoc.getDocumentElement();
            
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("IO Exception");
			e.printStackTrace(System.out);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Parser Configuration Exception");
			e.printStackTrace(System.out);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        
	}
	
	public DocumentBuilder getDocumentBuilder() throws Exception{
		if (docBuilder == null)
			throw new Exception("Document Builder equals null");
		return this.docBuilder;
	}
	
	public Document getDocument() throws Exception{
		if (drDoc == null)
			throw new Exception("Document equals null");
		return this.drDoc;
	}
	
	public Element getRoot() throws Exception{
		if (drRoot == null)
			throw new Exception("Root equals null");
		return this.drRoot;
	}
}

Open in new window

0
Comment
Question by:bored_shiva
  • 11
  • 11
  • 4
26 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24202066
does it work if you remove dtd

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24202276
>>XMLConfigurationException

You need to catch Throwable to see that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24202396
Check that you have no other Xerces xml classes in the classpath. Using the ext directory is often a cause of this kind of problem
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:bored_shiva
ID: 24202492
objects: by remove dtd I assume you mean removing the

CEHJ, I added a catch (Throwable t) {t.printStackTrace();} call. But I still see nothing. The classpath includes the Java JRE (1.6.7) and the project source files. Nothing else as far as I can see.

I added a -Djaxp.debug=1 argument to the JVM and I got this:
JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactoryJAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImplJAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl using ClassLoader: null
C:\Users\user\eclipse\ServerClient\Resources\config.xml

true
JAXP: using thread context class loader (sun.misc.Launcher$AppClassLoader@360be0) for searchJAXP: Looking up system property 'javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom'JAXP: The property is undefined.JAXP: found null in $java.home/jaxp.propertiesJAXP: no META-INF/services/javax.xml.xpath.XPathFactory file was foundJAXP: attempting to use the platform default W3C DOM XPath libJAXP: createInstance(com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl)JAXP: loaded com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl from jar:file:/C:/Program%20Files/Java/jre1.6.0_07/lib/rt.jar!/com/sun/org/apache/xpath/internal/jaxp/XPathFactoryImpl.classJAXP: factory 'com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl' was found for http://java.sun.com/jaxp/xpath/dom

This doesn't mean much to me, though. The two lines in the middle with the file path and the "true" are from the class code, obviously.

0
 

Author Comment

by:bored_shiva
ID: 24202506
sorry objects, I see that the line was cut short. I meant to say that I assume the remove dtd means remove the <!DOCTYPE entity entirely and remove the validation set to true. The answer is that it does not work that way either.  
0
 
LVL 92

Expert Comment

by:objects
ID: 24202512
try tunning it outside eclipse, ie. from the command line

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24202541
>>CEHJ, I added a catch (Throwable t) {t.printStackTrace();} call. But I still see nothing.

No - it's not a certainty you will - it could be consumed elsewhere
0
 

Author Comment

by:bored_shiva
ID: 24203031
objects: command line produces same results. I may point out that in running from command line I manually enter the filename, and the program identifies is just the same. (IE I get back the absolute path, and the check for fileExists() comes back true.

Any idea what's the http://apache.org/xml/features/standard-uri-conformant bit mean? by the name it looks as if it's a file location or file name string error, but I tried this in several different ways (including giving parse a URI and a FileStream) with the same results.

The most annoying thing is that this code worked previously, and then I turned the class it originally was in in into an abstract class with static methods. That was when it stopped working, but when I took the code out and put it in it's own concrete class, it didn't come back to life.  I'm attaching the calling class' code below, maybe that will clue you in.
Thanks.
package xml;
 
import java.io.File;
import java.io.IOException;
import xml.XMLNames;
import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.*;
import org.xml.sax.*;
 
public abstract class XMLReader {
 
	public static final String CONFIG_FILENAME = "Resources\\config.xml";
	private static String currentInputFile = null;
	private static Document currentInputDoc = null;
	private static Element currentInputRoot = null;
	private static Document configDoc = null;
	private static Element configRoot = null;
	private static XPath pathObj=null;
	private static XMLhead head=null;
	
	public static void XMLReaderInit(){
		File filename = new File(XMLReader.CONFIG_FILENAME);
		XMLReader.XMLhead head = loadXML(filename);
		XMLReader.setConfigDoc(head.getDoc());
		XMLReader.configRoot = head.getElm();
		XPathFactory xpf = XPathFactory.newInstance();
		XMLReader.pathObj= xpf.newXPath();
	}
	
	public static void XMLReaderInit(String filenameS){
		File filename = new File(filenameS);
		XMLReader.XMLhead head = loadXML(filename);
		XMLReader.setConfigDoc(head.getDoc());
		XMLReader.configRoot = head.getElm();
		XPathFactory xpf = XPathFactory.newInstance();
		XMLReader.pathObj= xpf.newXPath();
	}
	
	public static void loadInputFile(String filename){
		File fileFilename = new File(filename);
		XMLReader.loadInputFile(fileFilename);
//		XMLReader.XMLhead head = loadXML(fileFilename);
//		XMLReader.setCurrentInputDoc(head.getDoc());
//		XMLReader.currentInputRoot = head.getElm();
//		XMLReader.currentInputFile = filename;
	}
	
	public static void loadInputFile(File fileFilename){
		XMLReader.XMLhead head = loadXML(fileFilename);
		XMLReader.setCurrentInputDoc(head.getDoc());
		XMLReader.currentInputRoot = head.getElm();
		XMLReader.currentInputFile = fileFilename.toString();
	}
	
	private static XMLhead loadXML(File filename){
		Document XMLDoc;
		Element XMLRoot;
		XMLReader.XMLhead head=null;
		XMLDocumentResolver resolver = null;
//		File configFile = new File(filename);
		try {
//			System.out.println(filename.getCanonicalPath()+"\n");
//			System.out.println(filename.exists());
//			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            dbf.setValidating(true);
//            dbf.setIgnoringElementContentWhitespace(true);
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            
			resolver = new XMLDocumentResolver();
//			DocumentBuilder db = resolver.getDocumentBuilder();
//            XMLDoc = db.parse(filename);
//            XMLRoot = XMLDoc.getDocumentElement();
			resolver.initXMLDocumentResolver(filename);
            
            head = new XMLReader.XMLhead(resolver.getDocument(), resolver.getRoot());
            
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("IO Exception");
			e.printStackTrace(System.out);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Parser Configuration Exception");
			e.printStackTrace(System.out);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("SAX Exception");
			e.printStackTrace(System.out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return head;
	}
	
	public static String getConfigRoot(){
//		test method for testing XML retrival methods. Can be removed at the end.
/*		String answer=null;
		try {
				answer= XMLReader.pathObj.evaluate("/serverConfig/comm/listenPort",XMLReader.configDoc);
			} catch (XPathExpressionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return answer;*/
		return getElement(XMLNames.CONFIG_PORT,XMLNames.CONFIG_DOC);
	}
	
	public static String getElement(String question, Document doc){
//		takes a path (in the form of /a/path/to/node) and returns the value
		String answer=null;
		try {
				answer= XMLReader.pathObj.evaluate(question,doc);
			} catch (XPathExpressionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return answer;
	}
	
	public static void setConfigDoc(Document configDoc) {
		XMLReader.configDoc = configDoc;
	}
 
	public static Document getConfigDoc() {
		return configDoc;
	}
 
	public static void setCurrentInputDoc(Document currentInputDoc) {
		XMLReader.currentInputDoc = currentInputDoc;
	}
 
	public static Document getCurrentInputDoc() {
		return currentInputDoc;
	}
 
	private static class XMLhead{
		private Document doc = null;
		private Element elm = null;
		
		public XMLhead(Document d, Element e ){
			doc=d;
			elm=e;
		}
		
		public Document getDoc(){
			return doc;
		}
		
		public Element getElm(){
			return elm;
		}
	}
	
 
	
}//End XMLReader
 
 
	

Open in new window

0
 

Author Comment

by:bored_shiva
ID: 24203038
If anyone is curious, the code for xml.XMLNames is below
package xml;
 
import org.w3c.dom.Document;
 
public class XMLNames {
 
	public static final String CONFIG_PORT = "/serverConfig/com/listenPort";
	public static final String CONFIG_TIMEOUT = "serverConfig/com/timeoutSec";
	public static final Document CONFIG_DOC = XMLReader.getConfigDoc();
	public static final Document INPUT_DOC = XMLReader.getCurrentInputDoc();
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24203231
I'd be rolling back, and applying changes gradually testing between each step.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203326
I can't reproduce your error. Give your class a main method and try it stand-alone and tell use what you get
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203347
>>and try it stand-alone and tell use what you get

Should of course be

and try it stand-alone and tell us what you get. And by 'stand-alone' i mean at the command line again
0
 

Author Comment

by:bored_shiva
ID: 24203462
CEHJ, main class eclosed.
As I mentioned. Stand alone give exact same result.:

Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\user>cd eclipse\ServerClient\bin
C:\Users\user\eclipse\ServerClient\bin>java -cp . main "../resources/config.xml"

C:\Users\user\eclipse\ServerClient\Resources\config.xml

true


C:\Users\user\eclipse\ServerClient\bin>java -Djaxp.debug=1 -cp . main "../resour
ces/config.xml"
C:\Users\user\eclipse\ServerClient\Resources\config.xml

true
JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.Docume
ntBuilderFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.Docu
mentBuilderFactoryImpl using ClassLoader: null
JAXP: using thread context class loader (sun.misc.Launcher$AppClassLoader@ad3ba4
) for search
JAXP: Looking up system property 'javax.xml.xpath.XPathFactory:http://java.sun.c
om/jaxp/xpath/dom'
JAXP: The property is undefined.
JAXP: found null in $java.home/jaxp.properties
JAXP: no META-INF/services/javax.xml.xpath.XPathFactory file was found
JAXP: attempting to use the platform default W3C DOM XPath lib
JAXP: createInstance(com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl)
JAXP: loaded com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl from jar:fi
le:/C:/Program%20Files/Java/jre1.6.0_07/lib/rt.jar!/com/sun/org/apache/xpath/int
ernal/jaxp/XPathFactoryImpl.class
JAXP: factory 'com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl' was foun
d for http://java.sun.com/jaxp/xpath/dom


C:\Users\user\eclipse\ServerClient\bin>
import client.gui.ClientFrame;
import server.gui.ServerFrame;
import xml.XMLReader;
 
public class main {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClientFrame cf = new ClientFrame();
		ServerFrame sf = new ServerFrame();
		cf.setVisible(true);
		sf.setVisible(true);
		String filenameS=null;
		try {
			filenameS=args[0];
		}catch(ArrayIndexOutOfBoundsException aioobe){
			System.out.println("No arguments");
		}
		if (filenameS==null)
			XMLReader.XMLReaderInit();
		else 
			XMLReader.XMLReaderInit(filenameS);
		System.out.println(XMLReader.getConfigRoot());
	}
 
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203504
No, i meant give your originally posted class (XMLDocumentResolver) a main and run that standalone
0
 

Author Comment

by:bored_shiva
ID: 24204249
Same result when running stand alone. (both from within Eclipse and from the Command line). I added the main as listed below (the rest of XMLDocumentResolver remains exactly the same)
	public static void main(String[] args){
		System.out.println("running XMLDocumentResolver main");
		String filenameS=null;
		XMLDocumentResolver thisResolver = new XMLDocumentResolver();
		Document thisDoc=null;
		Element thisRoot = null;
		try {
			filenameS=args[0];
		}catch(ArrayIndexOutOfBoundsException aioobe){
			filenameS="Resources\\config.xml";
		}
		thisResolver.initXMLDocumentResolver(new File(filenameS));
		try {
			thisDoc = thisResolver.getDocument();
			thisRoot = thisResolver.getRoot();
			XPathFactory xpf = XPathFactory.newInstance();
			XPath pathObj = xpf.newXPath();
			System.out.println(pathObj.evaluate("/serverConfig/com/listenPort",thisDoc));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24204469
>>
      XPathFactory xpf = XPathFactory.newInstance();
                  XPath pathObj = xpf.newXPath();
                  System.out.println(pathObj.evaluate("/serverConfig/com/listenPort",thisDoc));
>>

You didn't have the above in your original code. Can you comment it out and tell us what you get then?
0
 

Author Comment

by:bored_shiva
ID: 24205196
Actually I did have it there.
Remember I told you the trouble started when I turned a class into an abstract class? It's there. The code for this class is below.
If you notice my original main refers to this class, and that's where it's suppose to get it's answers back from.

AS for this code specifically, it comes well after the parse statement, and so when I disabled it I still got the same problem.
Thanks
package xml;
 
import java.io.File;
import java.io.IOException;
import xml.XMLNames;
import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.*;
import org.xml.sax.*;
 
public abstract class XMLReader {
 
	public static final String CONFIG_FILENAME = "Resources\\config.xml";
	private static String currentInputFile = null;
	private static Document currentInputDoc = null;
	private static Element currentInputRoot = null;
	private static Document configDoc = null;
	private static Element configRoot = null;
	private static XPath pathObj=null;
	private static XMLhead head=null;
	
	public static void XMLReaderInit(){
		File filename = new File(XMLReader.CONFIG_FILENAME);
		XMLReader.XMLhead head = loadXML(filename);
		XMLReader.setConfigDoc(head.getDoc());
		XMLReader.configRoot = head.getElm();
		XPathFactory xpf = XPathFactory.newInstance();
		XMLReader.pathObj= xpf.newXPath();
	}
	
	public static void XMLReaderInit(String filenameS){
		File filename = new File(filenameS);
		XMLReader.XMLhead head = loadXML(filename);
		XMLReader.setConfigDoc(head.getDoc());
		XMLReader.configRoot = head.getElm();
		XPathFactory xpf = XPathFactory.newInstance();
		XMLReader.pathObj= xpf.newXPath();
	}
	
	public static void loadInputFile(String filename){
		File fileFilename = new File(filename);
		XMLReader.loadInputFile(fileFilename);
//		XMLReader.XMLhead head = loadXML(fileFilename);
//		XMLReader.setCurrentInputDoc(head.getDoc());
//		XMLReader.currentInputRoot = head.getElm();
//		XMLReader.currentInputFile = filename;
	}
	
	public static void loadInputFile(File fileFilename){
		XMLReader.XMLhead head = loadXML(fileFilename);
		XMLReader.setCurrentInputDoc(head.getDoc());
		XMLReader.currentInputRoot = head.getElm();
		XMLReader.currentInputFile = fileFilename.toString();
	}
	
	private static XMLhead loadXML(File filename){
		Document XMLDoc;
		Element XMLRoot;
		XMLReader.XMLhead head=null;
		XMLDocumentResolver resolver = null;
//		File configFile = new File(filename);
		try {
//			System.out.println(filename.getCanonicalPath()+"\n");
//			System.out.println(filename.exists());
//			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            dbf.setValidating(true);
//            dbf.setIgnoringElementContentWhitespace(true);
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            
			resolver = new XMLDocumentResolver();
//			DocumentBuilder db = resolver.getDocumentBuilder();
//            XMLDoc = db.parse(filename);
//            XMLRoot = XMLDoc.getDocumentElement();
			resolver.initXMLDocumentResolver(filename);
            
            head = new XMLReader.XMLhead(resolver.getDocument(), resolver.getRoot());
            
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("IO Exception");
			e.printStackTrace(System.out);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Parser Configuration Exception");
			e.printStackTrace(System.out);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("SAX Exception");
			e.printStackTrace(System.out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return head;
	}
	
	public static String getConfigRoot(){
//		test method for testing XML retrival methods. Can be removed at the end.
/*		String answer=null;
		try {
				answer= XMLReader.pathObj.evaluate("/serverConfig/comm/listenPort",XMLReader.configDoc);
			} catch (XPathExpressionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return answer;*/
		return getElement(XMLNames.CONFIG_PORT,XMLNames.CONFIG_DOC);
	}
	
	public static String getElement(String question, Document doc){
//		takes a path (in the form of /a/path/to/node) and returns the value
		String answer=null;
		try {
				answer= XMLReader.pathObj.evaluate(question,doc);
			} catch (XPathExpressionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return answer;
	}
	
	public static void setConfigDoc(Document configDoc) {
		XMLReader.configDoc = configDoc;
	}
 
	public static Document getConfigDoc() {
		return configDoc;
	}
 
	public static void setCurrentInputDoc(Document currentInputDoc) {
		XMLReader.currentInputDoc = currentInputDoc;
	}
 
	public static Document getCurrentInputDoc() {
		return currentInputDoc;
	}
 
	private static class XMLhead{
		private Document doc = null;
		private Element elm = null;
		
		public XMLhead(Document d, Element e ){
			doc=d;
			elm=e;
		}
		
		public Document getDoc(){
			return doc;
		}
		
		public Element getElm(){
			return elm;
		}
	}
	
 
	
}//End XMLReader
 
 
	

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24205516
>>Actually I did have it there.

I'm talking about your original code. xpath is not even among the imports there. I'm afraid it makes it very difficult to help you if you keep posting and attempting to discuss different code. You need to start out with the simplest test code and build it up incrementally.

Let's start again. Please post the simplest possible runnable code that fails in the way you mentioned
0
 

Author Comment

by:bored_shiva
ID: 24211831
Fair enough. The simplest running code I have that displays this is the standalone version of XMLDocument Resolver that you had me create. It is listed below in it's current form.

I see the problem when I debug the code, and then "step in" to drDoc = db.parse(filename);
This code uses the XML file above. The classpath includes just the JRE and sourcecode, and JVM arguments include just "-Djaxp.debug=1"
Thanks
package xml;
 
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.Element;
import org.xml.sax.SAXException;
 
public class XMLDocumentResolver{
	private DocumentBuilder docBuilder=null;
	private Document drDoc=null;
	private Element drRoot=null;
 
	public void initXMLDocumentResolver(File filename){
		try {
			System.out.println(filename.getCanonicalPath()+"\n");
			System.out.println(filename.exists());
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(true);
            dbf.setIgnoringElementContentWhitespace(true);
       
            docBuilder = dbf.newDocumentBuilder();
 
        	DocumentBuilder db = getDocumentBuilder();
            drDoc = db.parse(filename);
            drRoot = drDoc.getDocumentElement();
            
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("IO Exception");
			e.printStackTrace(System.out);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Parser Configuration Exception");
			e.printStackTrace(System.out);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable t){
			t.printStackTrace();
		}
        
	}
	
	public DocumentBuilder getDocumentBuilder() throws Exception{
		if (docBuilder == null)
			throw new Exception("Document Builder equals null");
		return this.docBuilder;
	}
	
	public Document getDocument() throws Exception{
		if (drDoc == null)
			throw new Exception("Document equals null");
		return this.drDoc;
	}
	
	public Element getRoot() throws Exception{
		if (drRoot == null)
			throw new Exception("Root equals null");
		return this.drRoot;
	}
	
	public static void main(String[] args){
		System.out.println("running XMLDocumentResolver main");
		String filenameS=null;
		XMLDocumentResolver thisResolver = new XMLDocumentResolver();
		try {
			filenameS=args[0];
		}catch(ArrayIndexOutOfBoundsException aioobe){
			filenameS="Resources\\config.xml";
		}
		thisResolver.initXMLDocumentResolver(new File(filenameS));
		try {
			Document thisDoc = thisResolver.getDocument();
			Element thisRoot = thisResolver.getRoot();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
} 

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213053
OK good. And can you post the complete jaxp.debug trace *from that code*?
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 24213067
easier to post what works, and I'll get someone to refactor it for (based on what you have here) you tomorrow. Saves trying to guess where the problem is.


0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 24213078
There's nothing wrong with your code of course ;-)  What you just posted works perfectly here.
0
 

Author Comment

by:bored_shiva
ID: 24213096
CEHJ:

C:\Users\user\eclipse\ServerClient\bin>java -cp . -Djaxp.debug=1 xml.XMLDocument
Resolver "../Resources/config.xml"
running XMLDocumentResolver main
C:\Users\usr\eclipse\ServerClient\Resources\config.xml

true
JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.Docume
ntBuilderFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.Docu
mentBuilderFactoryImpl using ClassLoader: null
0
 

Author Comment

by:bored_shiva
ID: 24213128
CEHJ:
That brings us back to the larger issue. If there's nothing wrong with the code for XMLDocumentResolver, then why do I not get back anything when I try to evaluate a xpath expression? (Both from within XMLDocumentResolver (when I enable those lines) and from XMLReader.)

At the end of the day it's probably going to be something stupid like not returning the information right, or some static variable wrinkle I missed. I just don't see it.  ;)
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213303
Hang on - let's back up a bit:

>> Please post the simplest possible runnable code that fails in the way you mentioned

What you've just posted does NOT fail - as you can see ...
0
 

Author Comment

by:bored_shiva
ID: 24213474
It works now!
The problem was never in the simplified version of XMLDocumentResolver, but in the greater fact that when I attempted to evaluate an XPath I'd get a null value. (This is why I kept posting parts of the bigger program, btw)  I just tried that again, and I guess in all the commenting and uncommenting  I did throughout this question I must have commented-out the problem. ;)  I'm still not sure what it was, but there are only a limited number of suspects, so I'll catch it eventually (after I commit this in it's current working state, that is)
Thanks to both of you for your time. I double the amount of points on this so I could award you both the full amount.
Thanks again
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question