I have some test code snippet:
import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
class Greet {
def name
Greet(who) { name = who[0].toUpperCase() +
who[1..-1] }
def salute() {
println "Hello !"
def input = """
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application xmlns:android="http://schemas.android.com/apk/res/android"
android:txt="this is origin">
<activity android:name="me.aolphn.MainActivity"/>
<activity xmlns:android="http://schemas.android.com/apk/res/android" android:configChanges ="me.aolphn.SecondActivity"/>
</application>
</manifest>
"""
def root = new XmlParser(false, true).parseText(input)
//def root = new XmlSlurper(false, true).parseText(input).declareNamespace(android:"http://schemas.android.com/apk/res/android")
//def writer = new StringWriter()
//root.'application'.attributes().put('@android:txt1','t1')
root.'application'[0].'activity'.each{act->
act.attributes()['android:configChanges']='txt aa'
}
println("========xxxxx:\n"+
XmlUtil.serialize(root))
//print writer.toString()
}
}
g = new Greet('world') // create object
g.salute()
If I run it online in here, the above code will encounter some exception, error message as following shows:
groovy.lang.GroovyRuntimeException: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 96; Attribute "configChanges" bound to namespace "http://schemas.android.com/apk/res/android" was already specified for element "activity".
at Greet.salute(Script1.groovy:29)
at Greet$salute.call(Unknown Source)
at Script1.run(Script1.groovy:35)
How fix this exception? Please help me. Any input will be appreciated.
After several hours,I fixed it by myself,here is correct way to do what I'm need.
The output result as following image