如何在空手道中插入具有特定索引的子节点 [英] How to insert a child node with specific index in Karate
本文介绍了如何在空手道中插入具有特定索引的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨,我正在尝试将一些节点插入到XML文件中
举个假例子
<Family>
<Father>
<Name>abc</Name>
<Age>4</Age>
<Gender>Male</Gender>
</Father>
<Mother>
<Name>bcd</Name>
<Age>5</Age>
<Gender>Female</Gender>
</Mother>
<Child>
<Name>bcd</Name>
<Age>5</Age>
<Gender>Female</Gender>
<Toy>
<Brand>def</Brand>
<Price>20</Price>
<Size>Middle</Size>
</Toy>
</Child>
</Family>
是否可以在Price节点后面添加一个节点?
或任何子节点重复多次?
我已尝试删除父"Toy"节点,并使用*Set重新构建该节点。但它似乎会在子节点的第一位置插入部分,而不是在性别节点之后* remove Family/Child/Toy
* set Family/Child/Toy =
"""
<Toy>
<Brand>def</Brand>
<Price>20</Price>
<Price>20</Price>
<Size>Middle</Size>
</Toy>
"""
<Family>
<Father>
<Name>abc</Name>
<Age>4</Age>
<Gender>Male</Gender>
</Father>
<Mother>
<Name>bcd</Name>
<Age>5</Age>
<Gender>Female</Gender>
</Mother>
<Child>
<Toy>
<Brand>def</Brand>
<Price>20</Price>
<Price>20</Price>
<Size>Middle</Size>
</Toy>
<Name>bcd</Name>
<Age>5</Age>
<Gender>Female</Gender>
</Child>
</Family>
因此,我当前的解决方案是删除整个Family部分,并将其替换为整个身体的一大块,以确保所有内容都将按正确的顺序放置。
我能问一下这个问题有没有更简单的解决方案?
非常感谢
xml
是的,操作推荐答案并非易事。也许一个字符串replace
就可以做到这一点。请注意,您可以将XPath与括号表示法结合使用来处理重复/嵌套的元素:
* def foo =
"""
<root>
<first>1</first>
<second>2</second>
</root>
"""
* replace foo.<second>2</second> = '<second>2</second><thirds><third>3.1</third></thirds>'
* xml foo = foo
* set foo /root/thirds/third[2] = '3.2'
* print foo
这将导致:
<root>
<first>1</first>
<second>2</second>
<thirds>
<third>3.1</third>
<third>3.2</third>
</thirds>
</root>
这篇关于如何在空手道中插入具有特定索引的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文