I'm using Dom4J to parse some Maven Pom files. When I use Pom files without a default namespace, everything works fine. For example:
Document pom = DocumentHelper.parseText(
"<project>" +
" <groupId>xx.gov.xxx.sistema.xxx</groupId>" +
" <artifactId>sis-teste</artifactId>" +
" <packaging>war</packaging>" +
"</project>");
//below works fine
String groupId = pom.selectSingleNode("/project/groupId").getText()
But if my Pom file defines a default namespace, it stops working:
Document pom = DocumentHelper.parseText(
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">" +
" <groupId>xx.gov.xxx.sistema.xxx</groupId>" +
" <artifactId>sis-teste</artifactId>" +
" <packaging>war</packaging>" +
"</project>");
//NullPointerException!!!!!!!!!!!!!!!!!!!!
String groupId = pom.selectSingleNode("/project/groupId").getText()
The weird thing is that pom.selectSingleNode("/project")
works fine.
How do I make my xpath query to work with the default namespace? I'd like to query just for "/project/groupId"
and get the groupId node.
My hacky solution was just to remove the namespace of the pom file before creating the Dom object. Not really beautiful, but it works fine and life's go on.