Hi,
I tried what you suggested but it still does not seem
to work for me.
I get a NullPointerException on the line of code that
calls getXmlElementUsingXPath()
String xpath =
"Document/[EMAIL PROTECTED]'Primary']/LOGIN/USERID";
String primaryUserId =
getXmlElementUsingXPath(inputDocument, xpath
).getText();
Here is the method that I defined using the code that
you provided
private static Element
getXmlElementUsingXPath(Document document,
String xpathExpression)
{
try
{
HashMap map = new HashMap();
map.put("",
"http://www.nget.com/coreapps/RunDVPRequest");
XPath xpath = new Dom4jXPath(xpathExpression);
xpath.setNamespaceContext(new
SimpleNamespaceContext(map));
return (Element)
xpath.selectSingleNode(document);
}
catch (JaxenException e)
{
System.err.println("Trouble with Jaxen...");
e.printStackTrace();
return null;
}
}
And here is the xml file that I am feeding it:
<Document
xmlns="http://www.nget.com/coreapps/RunDVPRequest">
<LOGIN_LIST filter="Primary">
<LOGIN>
<USERID>testuser1</USERID>
<PASSWORD>somepassword</PASSWORD>
</LOGIN>
</LOGIN_LIST>
</Document>
However, when I run the same peace of code without the
xmlns namespace declaration
<Document>
<LOGIN_LIST filter="Primary">
<LOGIN>
<USERID>testuser1</USERID>
<PASSWORD>somepassword</PASSWORD>
</LOGIN>
</LOGIN_LIST>
</Document>
It works fine and I don't get any errors.
Any ideas on how I can get the code to work without
errors?
Thanks.
--- Richard Eckart <[EMAIL PROTECTED]>
wrote:
> Hi,
>
> that is because your XPath statement operates on the
> default
> namespace (xmlns="") and when you set a different
> default namespace
> in your document the query will yield "null" as a
> result of the
> selectSingleNode method and your getText() will not
> work on null.
>
> Note that I only tried the following to define
> namespace prefixes,
> never to change the default namespace... but it
> probably would look
> something like this:
>
> try {
> HashMap<String, String> map = new HashMap<String,
> String>();
> // usually I put the prefix here as the first
> parameter
> map.put("",
> "http://www.nget.com/coreapps/RunDVPRequest");
>
> XPath xpath = new Dom4jXPath(xpathstring);
> xpath.setNamespaceContext(new
> SimpleNamespaceContext(map));
>
> return xpath.selectNodes(_xml);
> } catch (JaxenException e) {
> System.err.println("Trouble with Jaxen...");
> return null;
> }
>
> Cheers
>
> Richard
>
> Am 18.01.2007 um 00:07 schrieb S. Sharif:
>
**********************************************************
* Saladin Sharif
* e-mail: [EMAIL PROTECTED]
* Visit homepage @ http://gaia.ecs.csus.edu/~sharifs
**********************************************************
____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
dom4j-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dom4j-user