Groovy web console

subscribe to the feed Subscribe
to this
site

xml parse

Published 5 months ago by Ram with tags xmlslurper
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.xml.XmlUtil;
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document;
import javax.xml.transform.Transformer;

def body = '''<?xml version="1.0"?><Combined><source><PaymentExportRequest xmlns="urn:Ariba:Buyer:vsap" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" partition="prealm_3603" variant="vrealm_3603"><Payment_PaymentHeaderExport_Item><item><Amount><Currency><UniqueName>USD</UniqueName></Currency></Amount><InvoiceReconciliation><AdjustedTotalCostInERPPrecision><Amount>106.63000</Amount></AdjustedTotalCostInERPPrecision><BaselineDateInRequesterTimeZone>20220630</BaselineDateInRequesterTimeZone><BaselineDateString>20220630</BaselineDateString><CompanyCode><UniqueName>1000</UniqueName></CompanyCode><Invoice><InvoiceNumber>8200002458-2</InvoiceNumber><InvoicePurpose>standard</InvoicePurpose><IsPriceAdjustmentInvoice>false</IsPriceAdjustmentInvoice></Invoice><InvoiceDate>2022-06-30T19:06:26Z</InvoiceDate><PaymentTermsEffectiveDate>2022-06-30T19:06:26Z</PaymentTermsEffectiveDate><PaymentTermsEffectiveDateInRequesterTimeZone>06/30/2022 14:06:26-0500</PaymentTermsEffectiveDateInRequesterTimeZone><SAPInvoiceInd>X</SAPInvoiceInd><TaxAccrualAmount><Amount>0.00000</Amount></TaxAccrualAmount><UniqueName>IR8200002458-2-16688</UniqueName><TaxSource>ETE</TaxSource><custom><CustomBoolean name="IsFGInvoice">false</CustomBoolean><CustomBoolean name="WHTLineLevel">false</CustomBoolean><CustomDate name="InvoiceReceiptDate"/><CustomDate name="TaxReportingDate"/><CustomMoney name="StampDuty"/><CustomString name="AuthorizationNumber"/><CustomString name="BusinessPlace"/><CustomString name="CompanyCode">1000</CustomString><CustomString name="DocumentType"/><CustomString name="GrossAmount">106.625</CustomString><CustomString name="IBAN"/><CustomString name="IRN"/><CustomString name="ISRNumber"/><CustomString name="ISRReference"/><CustomString name="PaymentMethod"> </CustomString><CustomString name="PaymentNote"/><CustomString name="PlaceofSupply"/><CustomString name="Preparer">pavang</CustomString><CustomString name="PurchasingUnit">USA</CustomString><CustomString name="RemitToID">0001004218</CustomString><CustomString name="Requester">pavang</CustomString><CustomString name="SectionCode"/><CustomString name="SourceSystem"/><CustomString name="SupplierInvoiceNumber">8200002458-2</CustomString><CustomString name="TaxAmount"/><CustomString name="TaxCode"/><CustomString name="TaxExchRate"/><CustomString name="VATonDebits"/><CustomString name="additionalRemitInfo"/></custom></InvoiceReconciliation><PaymentAmounts><GrossAmount><Amount>106.62500</Amount></GrossAmount></PaymentAmounts><PaymentMethodType><UniqueName>L</UniqueName></PaymentMethodType><PaymentTerms><UniqueName>Z049</UniqueName></PaymentTerms><SupplierLocation><UniqueName>0001004218</UniqueName></SupplierLocation><UniqueName>PAY8200002458-2-16688</UniqueName></item></Payment_PaymentHeaderExport_Item></PaymentExportRequest></source><target><n0:ARBCIG_BAPI_INVOICE_CREATE xmlns:n0="urn:sap-com:document:sap:rfc:functions"><HEADERDATA><UNIQUEID>PAY8200002458-2-16688</UNIQUEID><INVOICE_IND>X</INVOICE_IND><DOC_TYPE>ZF</DOC_TYPE><DOC_DATE>2022-06-30</DOC_DATE><REF_DOC_NO>8200002458-2</REF_DOC_NO><COMP_CODE>1000</COMP_CODE><DIFF_INV>0001004218</DIFF_INV><CURRENCY>USD</CURRENCY><GROSS_AMOUNT>106.63</GROSS_AMOUNT><PMNTTRMS>Z049</PMNTTRMS><BLINE_DATE>2022-06-30</BLINE_DATE><HEADER_TXT>ARIBA_P2P</HEADER_TXT><DEL_COSTS>0.00</DEL_COSTS><PYMT_METH>L</PYMT_METH><ITEM_TEXT>IR8200002458-2-16688</ITEM_TEXT><TAX_SOURCE>ETE</TAX_SOURCE></HEADERDATA></n0:ARBCIG_BAPI_INVOICE_CREATE></target></Combined>'''

//def xml = new groovy.util.XmlParser().parseText(text)
//println xml.source

def combinedPayload = new XmlSlurper()
combinedPayload.setKeepIgnorableWhitespace(false)
combinedPayload = combinedPayload.parseText(body)
    
  def sourcePayload = combinedPayload.source
   sourcePayload = XmlUtil.serialize(sourcePayload)

print sourcePayload