XmlSlurper.appendNode 不改变大小

发布于 2024-11-13 09:25:44 字数 1236 浏览 4 评论 0原文

我使用 XmlSlurper 处理 XML。在我更新之前它工作得很好。 appendNode 不反映大小。

结构更新后如何使用 XmlSlurper?

XML 定义:

def CAR_RECORDS = '''
    <records>
      <car name='HSV Maloo' make='Holden' year='2006'>
        <country>Australia</country>
        <record type='speed'>Production Pickup Truck with speed of 271kph</record>
      </car>
      <car name='P50' make='Peel' year='1962'>
        <country>Isle of Man</country>
        <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
      </car>
      <car name='Royale' make='Bugatti' year='1931'>
        <country>France</country>
        <record type='price'>Most Valuable Car at $15 million</record>
      </car>
    </records>
  '''

代码女巫失败:

def records = new XmlSlurper().parseText(CAR_RECORDS)
records.appendNode( { car(make:'BMW') } ) 
assert 4 == records.car.size() //fails!!! size == 3

打印出包含 BMW 汽车的 XML

def xmlOut = new groovy.xml.StreamingMarkupBuilder()    
def temp = xmlOut.bind{ 
        mkp.yield records   
} 
​println temp​

I work with XML using XmlSlurper. It works fine until I update it. The appendNode doesn't reflect the size.

How to worked with XmlSlurper after structure update?

XML definition:

def CAR_RECORDS = '''
    <records>
      <car name='HSV Maloo' make='Holden' year='2006'>
        <country>Australia</country>
        <record type='speed'>Production Pickup Truck with speed of 271kph</record>
      </car>
      <car name='P50' make='Peel' year='1962'>
        <country>Isle of Man</country>
        <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
      </car>
      <car name='Royale' make='Bugatti' year='1931'>
        <country>France</country>
        <record type='price'>Most Valuable Car at $15 million</record>
      </car>
    </records>
  '''

Code witch fails:

def records = new XmlSlurper().parseText(CAR_RECORDS)
records.appendNode( { car(make:'BMW') } ) 
assert 4 == records.car.size() //fails!!! size == 3

Print out XML including BMW car

def xmlOut = new groovy.xml.StreamingMarkupBuilder()    
def temp = xmlOut.bind{ 
        mkp.yield records   
} 
​println temp​

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-11-20 09:25:44

您可以使用 XmlSlurper 再次读取新创建的结构。

...

records = new XmlSlurper().parseText(temp as String)
assert 4 == records.car.size()

You can read the newly created structure again using the XmlSlurper.

...

records = new XmlSlurper().parseText(temp as String)
assert 4 == records.car.size()
放我走吧 2024-11-20 09:25:44

尝试此

records = records.appendNode( { car(make:'BMW') } ) 

更新:实际上此代码无法编译(请参阅注释),因此我稍后会发布我自己的解决方法结果。

在某些项目中,我使用了模板字符串,例如:

static final String template = '<row><someList></someList></row>'

您可以像这样附加节点(lang Groovy):

    def row = new XmlSlurper().parseText(template);
    row.someList.appendNode(){
      ListElement(){
        field1('A')
        field2('B')
        field3('B')
      }
    }

Try this

records = records.appendNode( { car(make:'BMW') } ) 

update: actually this code doesn't compile(see comments) so i'll post my own workaround results later.

In some project i used template string like:

static final String template = '<row><someList></someList></row>'

You can append node like that(lang Groovy):

    def row = new XmlSlurper().parseText(template);
    row.someList.appendNode(){
      ListElement(){
        field1('A')
        field2('B')
        field3('B')
      }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文