Groovy: 如何查看xml中的多个标签值?
Groovy: How to check multiple tag value in xml?
我有xml这样的内容:
<?xml version="1.0" encoding="UTF-8"?><service>
<rs>
<Id>
<details>
<start>2017-10-07</start>
<startDate>2017-02-02</startDate>
<endDate>2017-03-02</endDate>
<runAs>false</runAs>
<makeVersion>1</makeVersion>
<patch>this is patch</patch>
<parameter>1</parameter>
</details>
</Id>
<person>
<details>
<start>2017-09-07</start>
<startDate>2017-02-02</startDate>
<endDate>2017-03-02</endDate>
<runAs>true</runAs>
<makeVersion>1</makeVersion>
<patch>this is patch</patch>
<parameter>1</parameter>
</details>
</person>
</rs>
<country>
<details>
<start>2017-09-07</start>
<startDate>2017-02-02</startDate>
<endDate>2017-03-02</endDate>
<runAs>true</runAs>
<makeVersion>1</makeVersion>
<patch>this is patch</patch>
<parameter>1</parameter>
</details>
</country>
</service>
我想检查每个起始标记值,然后更新 runAs 的值,因为我已经试过了:
def xml = new XmlParser().parseText(content)
def start=xml.'**'.details.start[0].text();
def run=xml.'**'.details.start[0].text();
if(start!=currentDate && run!='false'){
xml.'**'.details.runAs[0].value="false";
}
else {
xml.'**'.details.find({p->
p.start[0].value= subtractDays(p.start[0].text(),p.parameter[0].text()).toString()
p.runAs[0].value='false';
})
}
def newxml=XmlUtil.serialize(xml)
但它只更新了 rs->Id->details->runAs 值,我应该更改什么来更新每个 runAs
标签值,在某些情况下也更新每个开始标签值?
您可以使用以下代码将 runAs
元素值更改为 false
:
//Pass xml as string to parseText method
def xml = new XmlSlurper().parseText(xmlString)
//Find runAs element and change value to false
xml.'**'.findAll{it.name() == 'runAs'}.collect{it.replaceBody false}
println groovy.xml.XmlUtil.serialize(xml)
您可以快速在线试用demo
以同样的方式,您也可以通过在 findAll
闭包中提供条件来更改 start
元素值的值。
我有xml这样的内容:
<?xml version="1.0" encoding="UTF-8"?><service>
<rs>
<Id>
<details>
<start>2017-10-07</start>
<startDate>2017-02-02</startDate>
<endDate>2017-03-02</endDate>
<runAs>false</runAs>
<makeVersion>1</makeVersion>
<patch>this is patch</patch>
<parameter>1</parameter>
</details>
</Id>
<person>
<details>
<start>2017-09-07</start>
<startDate>2017-02-02</startDate>
<endDate>2017-03-02</endDate>
<runAs>true</runAs>
<makeVersion>1</makeVersion>
<patch>this is patch</patch>
<parameter>1</parameter>
</details>
</person>
</rs>
<country>
<details>
<start>2017-09-07</start>
<startDate>2017-02-02</startDate>
<endDate>2017-03-02</endDate>
<runAs>true</runAs>
<makeVersion>1</makeVersion>
<patch>this is patch</patch>
<parameter>1</parameter>
</details>
</country>
</service>
我想检查每个起始标记值,然后更新 runAs 的值,因为我已经试过了:
def xml = new XmlParser().parseText(content)
def start=xml.'**'.details.start[0].text();
def run=xml.'**'.details.start[0].text();
if(start!=currentDate && run!='false'){
xml.'**'.details.runAs[0].value="false";
}
else {
xml.'**'.details.find({p->
p.start[0].value= subtractDays(p.start[0].text(),p.parameter[0].text()).toString()
p.runAs[0].value='false';
})
}
def newxml=XmlUtil.serialize(xml)
但它只更新了 rs->Id->details->runAs 值,我应该更改什么来更新每个 runAs
标签值,在某些情况下也更新每个开始标签值?
您可以使用以下代码将 runAs
元素值更改为 false
:
//Pass xml as string to parseText method
def xml = new XmlSlurper().parseText(xmlString)
//Find runAs element and change value to false
xml.'**'.findAll{it.name() == 'runAs'}.collect{it.replaceBody false}
println groovy.xml.XmlUtil.serialize(xml)
您可以快速在线试用demo
以同样的方式,您也可以通过在 findAll
闭包中提供条件来更改 start
元素值的值。