如何为xml元素添加包含冒号(:)的属性,然后在groovy中对其进行序列化? [英] How to add an attribute which contains colon(:)for xml element and then serialize it in groovy?

查看:107
本文介绍了如何为xml元素添加包含冒号(:)的属性,然后在groovy中对其进行序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些测试代码片段:

I have some test code snippet:


​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()
​

我在此处在线运行,上面的代码会引起一些异常,错误消息如下所示

And I run it online in here,above code will encouter some exception,error message as follow showing

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元素名称空间的属性.例如,我想要一个元素'application'的属性'android:xxx',XmlUtil.serialize()添加后会遇到错误.帮我.任何责任将不胜感激.
    • Q:What I'm need?
    • A:I want add an attribute which contains namespace for xml element.As my example ,I want and an attribute 'android:xxx' for element 'application',XmlUtil.serialize() will encounter error after adding that.Please help me. Any responsibility will be appreciated.
    • 推荐答案

      最后,我通过XmlParser而不是'XmlSlurper'解决了此问题.以下是正确的方法.

      Finally I address this issue by XmlParser instead of 'XmlSlurper'.The following is right way.

      
      ​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()
      ​​​​​​​​​​​​​​​​​​​​​​​
      

      这篇关于如何为xml元素添加包含冒号(:)的属性,然后在groovy中对其进行序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆