Forum Discussion

SunkaraSuresh05's avatar
SunkaraSuresh05
New Contributor
5 years ago
Solved

How to create childNodes within childnode in sopaui (Using Groovy)

Hi,

I am trying to create a child nodes within a child node dynamically using groovy script as below:

 

<ns26:Entry>
ns26:ContactType>ABPerson</ns26:ContactType>
<ns26:FirstName>Axafoname</ns26:FirstName>
<ns26:LastName>Bcaio</ns26:LastName>
</ns26:Entry>

<ns26:Entry>
ns26:ContactType>ABPerson1</ns26:ContactType>
<ns26:FirstName>Axafoname1</ns26:FirstName>
<ns26:LastName>Bcaio12</ns26:LastName>
</ns26:Entry>

 

 

and so on....total 10 users. I am creating these nodes dynamically under parent tag <ns26:Results> and my code looks like as below:

allRowsDataForCreatedParnters.find { String line -> 
if(line.contains(Ibnnumber))
{
jsonObjectForPartner = jsonSlurper.parseText(line)
parentnode = holderIban.getDomNode("//*:Results")
xmldoc = parentnode.ownerDocument

mainChild = parentnode.appendChild(xmldoc.createElement("ns26:Entry"))
child = parentnode.appendChild(xmldoc.createElement("ns26:ContactType"))
child2 = parentnode.appendChild(xmldoc.createElement("ns26:FirstName"))
child3 = parentnode.appendChild(xmldoc.createElement("ns26:LastName"))

//printing values dynamically
child.appendChild(xmldoc.createTextNode(jsonObjectForPartner.ContactType))
child2.appendChild(xmldoc.createTextNode(jsonObjectForPartner.FirstName))
child3.appendChild(xmldoc.createTextNode(jsonObjectForPartner.LastName))

 

and my result is:

<ns26:Results>
<Entry/>
<ContactType/>
<FirstName>Derek</FirstName>
<LastName>Pace</LastName>
<LinkID>XTHCQG</LinkID>
<Entry/>
<ContactType/>
<FirstName>test</FirstName>
<LastName>testvv</LastName>

I am not getting how to create main node "<ns26:Entry>". Can anyone help me to get result like below:

Expected:

<Parent Tag>

  <Child Tag>

      <elements>

       <elements>

</child Tag>

<ns26:Results> -----Parent Tag
<ns26:Entry> --   Child tag
<ns:26ContactType/>
<ns:26FirstName>Derek</FirstName>
<ns:26LastName>Pace</LastName>
<ns:26LinkID>XTHCQG</LinkID>
</ns26:Entry>

<ns26:Entry>
<ns:26ContactType/>
<ns:26FirstName>test</FirstName>
<ns:26LastName>testvv</LastName>
</ns26:Entry>

</ns26:Results>

Attached actul expected result screenhot for reference 

 

 

 

 

 

  • Hello,

     

    Found the answer. I had to use the all children nodes 'appendChild' to main child node as follows:

    allRowsDataForCreatedParnters.find { String line ->  
    	
    	  if(line.contains(Ibnnumber))
    	  {
    	  	jsonObjectForPartner = jsonSlurper.parseText(line)
    	  	parentnode = holderIban.getDomNode("//*:Results")
    	  	xmldoc = parentnode.ownerDocument 
    		childentry = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"Entry"))
    		child = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"ContactType"))
    		child2 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"DateOfBirth"))
    		child3 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"FirstName"))
    		child4 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"LastName"))
    		child5 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"LinkID"))
    		child6 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"Prefix"))
    		child7 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"AddressLine1"))
    		child8 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"City"))
    		child9 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"Postal"))
    		child10 = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"Gender_De"))
    	
    		child.appendChild(xmldoc.createTextNode(jsonObjectForPartner.ContactType))
    		childentry.appendChild(child)
    		child2.appendChild(xmldoc.createTextNode(jsonObjectForPartner.DateOfBirth))
    		childentry.appendChild(child2)
    		child3.appendChild(xmldoc.createTextNode(jsonObjectForPartner.FirstName))
    		childentry.appendChild(child3)
    		child4.appendChild(xmldoc.createTextNode(jsonObjectForPartner.LastName))
    		childentry.appendChild(child4)
    		child5.appendChild(xmldoc.createTextNode(jsonObjectForPartner.LinkID))
    		childentry.appendChild(child5)
    		child6.appendChild(xmldoc.createTextNode(jsonObjectForPartner.Prefix))
    		childentry.appendChild(child6)
    		child7.appendChild(xmldoc.createTextNode(jsonObjectForPartner.AddressLine1))
    		childentry.appendChild(child7)
    		child8.appendChild(xmldoc.createTextNode(jsonObjectForPartner.City))
    		childentry.appendChild(child8)
    		child9.appendChild(xmldoc.createTextNode(jsonObjectForPartner.PostalCode))
    		childentry.appendChild(child9)
    		child10.appendChild(xmldoc.createTextNode(jsonObjectForPartner.Gender))
    		childentry.appendChild(child10)

    Thank you so much nmrao for your quick responses. 

6 Replies

    • SunkaraSuresh05's avatar
      SunkaraSuresh05
      New Contributor

      Hello,

       

      Sorry, i might not have provided my query details clearly. here is my query details in short:

      I just tried to get name spaces for the child nodes using below script line and got result successfully:

      child = parentnode.appendChild(xmldoc.createElementNS(parentnode.getNamespaceURI(),"ContactType"))

       

      and now my concern is could not able to wrapup my childnodes under

      <ns:Entry> 

      -----

      -----

      </ns:Entry>

       

      How to create this main child node "<ns:Entry>" ?

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Please refer earlier reply and answer the requested info which will help to understand the issue.