如何为 xml 元素添加一个包含冒号 (:) 的属性,然后在 groovy 中序列化它?
How to add an attribute which contains colon(:)for xml element and then serialize it in groovy?
我有一些测试代码片段:
import groovy.xml.XmlUtil
class Greet {
Greet() { }
def salute() {
println "Hello !"
def input = """
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application >
<activity android:name="me.aolphn.MainActivity">
</activity>
</application>
</manifest>
"""
// def root = new XmlParser(false, true).parseText(input)
def root = new XmlSlurper(false, true).parseText(input)
root.'application'.@'android:txt'='this is txt'
XmlUtil.serialize(root)
}
}
g = new Greet() // create object
g.salute()
而我在here在线运行,上面的代码会遇到一些异常,报错信息如下
groovy.lang.GroovyRuntimeException: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 24; Element type "application" must be followed by either attribute specifications, ">" or "/>".
at Greet.salute(Script1.groovy:24)
at Greet$salute.call(Unknown Source)
at Script1.run(Script1.groovy:29)
- 问:我需要什么?
- A:我想添加一个属性,其中包含 xml element.As 我的示例的命名空间,我想要一个属性 'android:xxx' 用于元素 'application',XmlUtil.serialize() 添加 that.Please 后会遇到错误帮助我。如有任何责任,我们将不胜感激。
最后我用XmlParser
而不是'XmlSlurper'来解决这个问题。下面是正确的方法。
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="com.cloud.chsocialtest.MainActivity">
</activity>
</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].attributes()['android:new']='txt aa'
println("========:\n"+
XmlUtil.serialize(root))
//print writer.toString()
}
}
g = new Greet('world') // create object
g.salute()
我有一些测试代码片段:
import groovy.xml.XmlUtil
class Greet {
Greet() { }
def salute() {
println "Hello !"
def input = """
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application >
<activity android:name="me.aolphn.MainActivity">
</activity>
</application>
</manifest>
"""
// def root = new XmlParser(false, true).parseText(input)
def root = new XmlSlurper(false, true).parseText(input)
root.'application'.@'android:txt'='this is txt'
XmlUtil.serialize(root)
}
}
g = new Greet() // create object
g.salute()
而我在here在线运行,上面的代码会遇到一些异常,报错信息如下
groovy.lang.GroovyRuntimeException: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 24; Element type "application" must be followed by either attribute specifications, ">" or "/>".
at Greet.salute(Script1.groovy:24)
at Greet$salute.call(Unknown Source)
at Script1.run(Script1.groovy:29)
- 问:我需要什么?
- A:我想添加一个属性,其中包含 xml element.As 我的示例的命名空间,我想要一个属性 'android:xxx' 用于元素 'application',XmlUtil.serialize() 添加 that.Please 后会遇到错误帮助我。如有任何责任,我们将不胜感激。
最后我用XmlParser
而不是'XmlSlurper'来解决这个问题。下面是正确的方法。
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="com.cloud.chsocialtest.MainActivity">
</activity>
</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].attributes()['android:new']='txt aa'
println("========:\n"+
XmlUtil.serialize(root))
//print writer.toString()
}
}
g = new Greet('world') // create object
g.salute()