从以下XML解析属性值有问题:
s='''<?xml version="1.0" encoding="UTF-8"?>
<web-ext
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd"
version="1.0">
<reload-interval value="3"/>
<context-root uri="foo/bar" />
<enable-directory-browsing value="false"/>
<enable-file-serving value="true"/>
<enable-reloading value="true"/>
<enable-serving-servlets-by-class-name value="false" />
</web-ext>
'''
def contextroot
def xml = new XmlParser(false,false).parseText(s)
xml.each {
if (it.name() == "context-root")
contextroot = it.attributes().uri
}它给了我正确的价值。但是否有更直接的方法?有点像
xml.name("context-root").uri不起作用。
发布于 2018-09-14 16:48:50
如果您想直接访问这个属性,可以使用
xml.'context-root'[0].@urihttps://stackoverflow.com/questions/52335927
复制相似问题