Skip to content

Instantly share code, notes, and snippets.

@playerjamesbattleground
Created May 12, 2014 03:24
Show Gist options
  • Select an option

  • Save playerjamesbattleground/42a98a420c6f91f76792 to your computer and use it in GitHub Desktop.

Select an option

Save playerjamesbattleground/42a98a420c6f91f76792 to your computer and use it in GitHub Desktop.
groovy xml 2 json, exact match
xml ='''
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://api.directconnect.com.au/">
<soapenv:Header/>
<soapenv:Body>
<api:addCase soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<newCase xsi:type="api:CaseData">
<!--You may enter the following 38 items in any order-->
<DcamApiUserId xsi:type="xsd:string">123</DcamApiUserId>
<DcamApiToken xsi:type="xsd:string">111</DcamApiToken>
<TitleApplicant1 xsi:type="xsd:string">111</TitleApplicant1>
<FirstNameApplicant1 xsi:type="xsd:string">111</FirstNameApplicant1>
<LastNameApplicant1 xsi:type="xsd:string">111</LastNameApplicant1>
<DateOfBirthApplicant1 xsi:type="xsd:string">111</DateOfBirthApplicant1>
<EmailAddressApplicant1 xsi:type="xsd:string">111</EmailAddressApplicant1>
<MobilePhoneApplicant1 xsi:type="xsd:string">111</MobilePhoneApplicant1>
<HomePhoneApplicant1 xsi:type="xsd:string">111</HomePhoneApplicant1>
<WorkPhoneApplicant1 xsi:type="xsd:string">111</WorkPhoneApplicant1>
<OtherPhoneApplicant1 xsi:type="xsd:string">111</OtherPhoneApplicant1>
<TitleApplicant2 xsi:type="xsd:string">111</TitleApplicant2>
<FirstNameApplicant2 xsi:type="xsd:string">111</FirstNameApplicant2>
<LastNameApplicant2 xsi:type="xsd:string">111</LastNameApplicant2>
<DateOfBirthApplicant2 xsi:type="xsd:string">111</DateOfBirthApplicant2>
<EmailAddressApplicant2 xsi:type="xsd:string">111</EmailAddressApplicant2>
<MobilePhoneApplicant2 xsi:type="xsd:string">111</MobilePhoneApplicant2>
<HomePhoneApplicant2 xsi:type="xsd:string">111</HomePhoneApplicant2>
<WorkPhoneApplicant2 xsi:type="xsd:string">111</WorkPhoneApplicant2>
<OtherPhoneApplicant2 xsi:type="xsd:string">111</OtherPhoneApplicant2>
<NewAddressUnitNumber xsi:type="xsd:string">111</NewAddressUnitNumber>
<NewAddressStreetNumber xsi:type="xsd:string">111</NewAddressStreetNumber>
<NewAddressStreetName xsi:type="xsd:string">111</NewAddressStreetName>
<NewAddressStreetType xsi:type="xsd:string">111</NewAddressStreetType>
<NewAddressSuburb xsi:type="xsd:string">111</NewAddressSuburb>
<NewAddressState xsi:type="xsd:string">111</NewAddressState>
<NewAddressPostCode xsi:type="xsd:int">111</NewAddressPostCode>
<NewUnstructuredAddress xsi:type="xsd:string">111</NewUnstructuredAddress>
<PreviousAddressUnitNumber xsi:type="xsd:string">111</PreviousAddressUnitNumber>
<PreviousAddressStreetNumber xsi:type="xsd:string">111</PreviousAddressStreetNumber>
<PreviousAddressStreetName xsi:type="xsd:string">111</PreviousAddressStreetName>
<PreviousAddressStreetType xsi:type="xsd:string">111</PreviousAddressStreetType>
<PreviousAddressSuburb xsi:type="xsd:string">111</PreviousAddressSuburb>
<PreviousAddressState xsi:type="xsd:string">111</PreviousAddressState>
<PreviousAddressPostCode xsi:type="xsd:int">111</PreviousAddressPostCode>
<ConnectionDate xsi:type="xsd:string">111</ConnectionDate>
<CommissionToPropertyManagerStaffId xsi:type="xsd:string">111</CommissionToPropertyManagerStaffId>
<SourceName xsi:type="xsd:string">111</SourceName>
</newCase>
</api:addCase>
</soapenv:Body>
</soapenv:Envelope>
'''
def parsed = new XmlParser().parseText(xml)
def getLocalName(n) {
if(n.name() instanceof groovy.xml.QName)
return n.name().getLocalPart()
else
return n.name()
}
def handle
handle = { node ->
//println node
if( node instanceof String ) {
node
}
else {
[ (getLocalName(node)): node.collect( handle ) ]
}
}
def jsonObject = [ (getLocalName(parsed)): parsed.collect { node ->
[(getLocalName(node)) : node.collect( handle ) ]
} ]
println jsonObject
def json = new groovy.json.JsonBuilder( jsonObject )
groovy.json.JsonOutput.prettyPrint(json.toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment