【问题标题】:How to insert a child node with specific index in Karate如何在空手道中插入具有特定索引的子节点
【发布时间】:2019-11-26 22:46:20
【问题描述】:

您好,我正在尝试将一些节点插入到 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>     

我可以在价格节点之后添加一个节点吗?

或者是否有任何子节点重复多次?

我已经尝试删除父“玩具”节点并使用 * 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> 

所以我目前的解决方案是删除整个家庭部分并用整个身体的一部分替换它,以确保所有内容都按正确的顺序放置。

请问有没有更简单的解决方法?

非常感谢

【问题讨论】:

    标签: xml api testing karate


    【解决方案1】:

    是的,操作 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>
    

    【讨论】:

    • 感谢您回答问题!但我还是有点困惑。如果我想进入更深层次,请更改 节点中的一个子节点(),我该如何使用替换来管理这个?
    • 嗨彼得,你能看看我在下面添加的答案吗?谢谢:)
    • @Jackson 不,您不能“在其间插入节点”,因此请使用 replace 或覆盖整个“父”节点。 XML 中的重复节点可以在 XPath 中完成,如上所示。
    【解决方案2】:

    根据彼得的回答,您将需要使用替换在元素“a”和“b”之间插入。见下文

          * def foo =
      """
        <root>
          <first>1</first>
          <second>2</second>
          <thirds>
            <a>a</a>
            <third>3.1</third>
            <b>b</b>
          </thirds>
          <fourth>4</fourth>
        </root>
      """
    
      * replace foo.<third>3.1</third> = '<third>3.1</third><third>inserted between A and B</third>'
      * xml foo = foo
      * print foo
    

    输出:

    <root>
      <first>1</first>
      <second>2</second>
      <thirds>
        <a>a</a>
        <third>3.1</third>
        <third>added between A and B</third>
        <b>b</b>
      </thirds>
      <fourth>4</fourth>
    </root>
    

    【讨论】:

    • 谢谢蒂姆,这正是我要找的:D
    【解决方案3】:

    抱歉,彼得,我必须创建另一个答案来格式化内容

    我已经尝试了您提供的示例,它确实有效。但我的情况更像

    <root>
      <first>1</first>
      <second>2</second>
      <thirds>
        <a>a</a>
        <third>3.1</third>
        <b>b</b>
      </thirds>
      <fourth>4</fourth>
    </root>
    

    我正在尝试在“a”和“b”节点之间添加另一个节点

    如果我按照你的建议添加了一个节点

    * set foo /root/thirds/third[2] = '3.2'
    * set foo /root/thirds/third[3] = '3.2'
    

    那么第二个节点会出现在其父节点的底部

    <root>
       <first>1</first>
       <second>2</second>
       <thirds>
          <a>a</a>
          <third>3.1</third>
          <b>b</b>
          <third>3.2</third>
          <third>3.2</third>
       </thirds>
       <fourth>4</fourth>
    </root>
    

    所以如果我需要将节点连续复制多次,或者在“a”和“b”节点之间插入,有可能吗?

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 2021-09-14
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多