<?xml version="1.0" encoding="UTF-8" ?>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
解析代码
class MavenXmlParseDemo {
public static void main(String[] args) {
def input = MavenXmlParseDemo.class.getResourceAsStream("/resources/maven.xml")
def dependencies = new XmlSlurper().parse(input)
for(dependency in dependencies.dependency){
println "groupId:${dependency.groupId} artifactId:${dependency.artifactId} version:${dependency.version}"
}
}
}
<persons>
<person id="1" name="king">
<address>hangzhou</address>
</person>
<person id="2" name="kong">
<address>zhoushan</address>
</person>
</persons>
attr解析
class PersonXmlParseDemo {
public static void main(String[] args) {
def input = MavenXmlParseDemo.class.getResourceAsStream("/resources/person.xml")
def persons = new XmlSlurper().parse(input)
for(p in persons.person){
println "id:${p.@id} name:${p.@name} address:${p.address}"
}
}
}