<msg:Request xmlns:msg="urn:some.namespace">
<msg:Metadata msg:RequestGuid=""/>
</msg:Request>
我打算阅读这个静态XML模板并使用XMLParser更新msg:RequestGuid。
我该怎么做?
我所拥有的不起作用,失败了 java.lang.NullPointerException:groovy.lang.ReadOnlyPropertyException:无法为类设置readonly属性:value:java.lang.String
def Request = new XmlParser().parseText(Utils.readFile(this.class, "templates/rq.xml"));
Node requestGuid = RetrieveRequest.attribute(msg.RequestGuid);
requestGuid.value = UUID.randomUUID();
答案 0 :(得分:2)
也许有一种更简单的方法来获取属性,但我还没有看到它
def request = new XmlParser().parseText('''\
<msg:Request xmlns:msg="urn:some.namespace">
<msg:Metadata msg:RequestGuid=""/>
</msg:Request>''')
// the ns, be sure to copy the uri properly
def msg = new groovy.xml.Namespace('urn:some.namespace', 'msg')
def metadata = request[msg.Metadata][0]
// only good for reading
println metadata.attribute(msg.RequestGuid) // ""
// writing must be done via the map of attributes
metadata.attributes()[msg.RequestGuid] = 'theuid'
println metadata.attribute(msg.RequestGuid) // theuid
new XmlNodePrinter().print(request)
//<msg:Request xmlns:msg="urn:some.namespace">
// <msg:Metadata msg:RequestGuid="theuid"/>
//</msg:Request>