How to add features to XML using groovy

Source: Internet
Author: User

How do I add features to XML using groovy?


Ask:

In groovy, I need to add an attribute (attribute) to the root element of the XML. I want to use Xmlslurper. What should I do? Adding elements is very simple.

For:

Run the following code in groovy Console with good results.

Import groovy.xml.streamingmarkupbuilder//the original xmldef input = "<foo><bar></bar></foo > "//Add Attributename=" AttributeValue "to the rootdef root = new Xmlslurper (). ParseText (input) [email protected] = ' att Ributevalue '//Get the modified XML and check that it workeddef Outputbuilder = new Streamingmarkupbuilder () String updated Xml = outputbuilder.bind{Mkp.yield root}assert "<foo attributename= ' AttributeValue ' ><bar></bar> </foo> "= = Updatedxml

Adding an attribute is the same as reading a feature:

Import groovy.xml.StreamingMarkupBuilderdef input = ' <thing> <more> </more></thing> ' def root = new Xmlslurper (). ParseText (input) [email protected] = ' new ' def outputbuilder = new Streamingmarkupbuilder () String R Esult = outputbuilder.bind{Mkp.yield root}println result

will be generated:

<thing stuff= ' new ' ><more></more></thing>

Source: >


How to add features to XML using groovy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.