Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using DOM4J XPath

807580Apr 6 2010 — edited Apr 6 2010
IM going nuts trying to use DOM4J to query something so simple. Here is the input XML file and the code to run the XPath. If I loop through the document using the annoying element and node map thing it works fine and I can get all the nodes. But I want to run an XPath query right to the information I want. Firs time I tried without the URI nodes and this time I tried it with it. Still no luck.

XML FILE
<?xml version="1.0"?>
<configs>
	<config SERVICE="Service Name">
		<detail name="key">value</detail>
	</config>
	<config SERVICE="ProcessErrors">
		<detail name="ErrorEmailAddresses">johnsmith@hotmail.com</detail>
	</config>
	<config SERVICE="TestSQLErrors">
		<detail name="ErrorEmailAddresses">johnsmith@hotmail.com</detail>
	</config>	
</configs>
CODE
File fXmlFile = new File("n:\\config.xml");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
 
XPath xpathNodes = new DefaultXPath("//configs/config[@SERVICE='ProcessErrors']/detail[@name='ErrorEmailAddresses']");
HashMap<String, String> mapNodes = new HashMap<String,String>();
xpathNodes.setNamespaceURIs(mapNodes);		
Node exceptionNode = (Node)xpathNodes.selectSingleNode(doc);
System.out.println(exceptionNode);
exceptionNode is empty. If I run this same query on the same document in XML spy I get the value.???
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 4 2010
Added on Apr 6 2010
3 comments
440 views