Jackson
Jackson

Reputation: 101

How to insert a child node with specific index in Karate

Hi I was trying to insert some nodes into a xml file

Take a dummy example

<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>     

Is it possible to that I could add a node right after the Price node?

Or have any of the child nodes duplicated for multiple times?

I've tried remove the parent 'Toy' node and use * set to rebuild that. But it seems like it will insert the section in the first place of the Child node, instead of after the Gender node

* 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> 

So my current solution is to remove the whole Family section and replace it with a chunk of the whole body to make sure everything will be placed in the right order.

Can I ask is there any easier solution for this?

Many thanks

Upvotes: 2

Views: 701

Answers (2)

Tim
Tim

Reputation: 41

As per Peter's answer you will need to use replace to insert between elements 'a' & 'b'. See below

      * 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

Outputs:

<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>

Upvotes: 2

Peter Thomas
Peter Thomas

Reputation: 58088

Yes manipulating XML is not easy. Maybe a string replace can do the trick. And note that you can use XPath with the bracket notation to handle repeating / nested elements:

* 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

Which results in:

<root>
  <first>1</first>
  <second>2</second>
  <thirds>
    <third>3.1</third>
    <third>3.2</third>
  </thirds>
</root>

Upvotes: 1

Related Questions