Forum Discussion

anand1's avatar
anand1
Contributor
12 years ago

[Resolved] Replace node in XML

I have a XML which contains multiple tags, the hierarchy is somewhat like this
<PlayerInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/nConnect.Resources.PlayerInfo.Entities">
<ClientID i:nil="true"/>
<DateTime>0001-01-01T00:00:00</DateTime>
<ErrorCode i:nil="true"/>
<ErrorText i:nil="true"/>
<PropertyId i:nil="true"/>
<ReplyToSystem i:nil="true"/>
<UserID i:nil="true"/>
<VendorName i:nil="true"/>
<Address>
<PlayerAddress>
<AddressActive>true</AddressActive>
<AddressCareOf>Naomi Campbell</AddressCareOf>
<AddressCity>Little Rock</AddressCity>
<AddressComment>**Mailing Comments**</AddressComment>
<AddressId i:nil="true"/>
<AddressLine1>111 Street</AddressLine1>
<AddressLine2>112 Street</AddressLine2>
<AddressLine3>113 Street</AddressLine3>
<AddressLine4>114 Street</AddressLine4>
<AddressSuburb>Sector-11</AddressSuburb>
<AddressType>Mail<AddressType>
<CountryCode>US</CountryCode>
</PlayerAddress>
</PlayerInfo>
Now i have to change the value of AddressType from "mail" to <AddressType i:nil="true"/> in groovy, how should I go about it.
I can easily remove this node, however not able to replace the same.

2 Replies

  • Hi,

    Custom groovy solutions are not part of are support agreement as a FYI.

    You can try to use the XMLObject api to set the value to nil.
    http://xmlbeans.apache.org/docs/2.0.0/r ... bject.html

    Example:


    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder(context.httpResponse.responseContent)
    def xmlObj = holder.getXmlObject(); //this will return org.apache.xmlbeans.XmlObject
    def addressType = xmlObj.selectPath(
    "declare namespace xmlns:i='http://www.w3.org/2001/XMLSchema-instance' p='http://schemas.datacontract.org/2004/07/nConnect.Resources.PlayerInfo.Entities' " +
    "//p:PlayerInfo/p:Address/p:AddressType")
    addressType[0].setNil()
    def holderNew = new XmlHolder( xmlObj.set(addressType[0]) ) //returns a new XmlHolder with value set to nil



    Regards,
    Marcus
    SmartBear Support
  • Thanks Marcus for providing the direction for it.....Will try to move and achieve what i wanted to.....