I'm trying to parse and modify a Maven's pom.xml using Groovy's XmlSlurper. My pom.xml declares the namespace xsi.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>a-group-id</groupId>
<artifactId>an-artifact-id</artifactId>
My Groovy source is as follows:
import groovy.xml.XmlUtil
def pom = new XmlSlurper().parse('pom.xml')
.declareNamespace('': 'http://maven.apache.org/POM/4.0.0',
xsi: 'http://www.w3.org/2001/XMLSchema-instance')
//manipulate the pom
println XmlUtil.serialize(pom)
As you notice, I've declared the first namespace as empty. However in the output tag0 is added everywhere.
<?xml version="1.0" encoding="UTF-8"?>
<tag0:project xmlns:tag0="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">
<tag0:modelVersion>4.0.0</tag0:modelVersion>
<tag0:groupId>a-group-id</tag0:groupId>
<tag0:artifactId>an-artifact-id</tag0:artifactId>
How to avoid that?
For the moment my workaround is removing the tags manually:
println XmlUtil.serialize(pom).replaceAll('tag0:', '').replaceAll(':tag0', '')
XmlSlurper
with no namespace support godd enough? ie:println XmlUtil.serialize( new XmlSlurper( false, false ).parse( 'pom.xml' ) )
? – PamplonaXmlParser
and seems to be heading the direction we want, but so far no joy of getting it to work :-/ Not sure if it's a parsing problem or one of serialization... – Pamplona