XML Parse - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of XML Parse.php

  1. <?php
  2. $resp='{
  3.   "Id": "b7676489-7f0b-495c-a80f-74437b95dd6e",
  4.   "Status": "OK",
  5.   "ProviderName": "API Explorer",
  6.   "DateTimeUTC": "\/Date(1633543239638)\/",
  7.   "Invoices": [
  8.     {
  9.       "Type": "ACCREC",
  10.       "InvoiceID": "07f4e2b5-7693-4506-b1b9-585d2b1ce316",
  11.       "InvoiceNumber": "INV-0042",
  12.       "Reference": "Website Design",
  13.       "Prepayments": [],
  14.       "Overpayments": [],
  15.       "AmountDue": 40.00,
  16.       "AmountPaid": 0.00,
  17.       "SentToContact": false,
  18.       "CurrencyRate": 1.0000000000,
  19.       "IsDiscounted": false,
  20.       "HasErrors": false,
  21.       "Contact": {
  22.         "ContactID": "ea5f94be-d51c-4775-b8cc-fe70539aa915",
  23.         "ContactNumber": "9d788d880f299b1eaaa4def1066afc04883126eabbd99574d6",
  24.         "ContactStatus": "ACTIVE",
  25.         "Name": "Shawn Fernandes",
  26.         "FirstName": "Shawn",
  27.         "LastName": "Fernandes",
  28.         "EmailAddress": "[email protected]",
  29.         "Addresses": [
  30.           {
  31.             "AddressType": "POBOX"
  32.           },
  33.           {
  34.             "AddressType": "STREET"
  35.           }
  36.         ],
  37.         "Phones": [
  38.           {
  39.             "PhoneType": "DEFAULT"
  40.           },
  41.           {
  42.             "PhoneType": "DDI"
  43.           },
  44.           {
  45.             "PhoneType": "FAX"
  46.           },
  47.           {
  48.             "PhoneType": "MOBILE"
  49.           }
  50.         ],
  51.         "UpdatedDateUTC": "\/Date(1633052796037+0000)\/",
  52.         "ContactGroups": [],
  53.         "IsSupplier": false,
  54.         "IsCustomer": true,
  55.         "SalesTrackingCategories": [],
  56.         "PurchasesTrackingCategories": [],
  57.         "ContactPersons": [],
  58.         "HasValidationErrors": false
  59.       },
  60.       "DateString": "2019-03-11T00:00:00",
  61.       "Date": "\/Date(1552262400000+0000)\/",
  62.       "DueDateString": "2018-12-10T00:00:00",
  63.       "DueDate": "\/Date(1544400000000+0000)\/",
  64.       "BrandingThemeID": "324587a9-7eed-46c0-ad64-fa941a1b5b3e",
  65.       "Status": "AUTHORISED",
  66.       "LineAmountTypes": "Exclusive",
  67.       "LineItems": [
  68.         {
  69.           "Description": "Acme Tires",
  70.           "UnitAmount": 20.00,
  71.           "TaxType": "NONE",
  72.           "TaxAmount": 0.00,
  73.           "LineAmount": 40.00,
  74.           "AccountCode": "220",
  75.           "Tracking": [],
  76.           "Quantity": 2.0000,
  77.           "LineItemID": "6ab5aae1-d5ee-4de9-8150-54d13c4a3977",
  78.           "ValidationErrors": []
  79.         }
  80.       ],
  81.       "SubTotal": 40.00,
  82.       "TotalTax": 0.00,
  83.       "Total": 40.00,
  84.       "UpdatedDateUTC": "\/Date(1633543239193+0000)\/",
  85.       "CurrencyCode": "USD"
  86.     }
  87.   ]
  88. }';
  89. //$resp=json_encode($resp);
  90. //var_dump($resp);
  91. $json_resp2 = json_decode($resp,true);
  92.        
  93. //Parse invoice id from response
  94. $invoice_id= $json_resp2['Invoices'][0]['InvoiceID'];
  95.  
  96. echo $invoice_id."\n";
  97. echo "ln 3 \n";
  98. echo "ln 4 \n";
  99.  
  100. $resp2='<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Id>84e042a2-acf0-4d18-ac53-8ee15f7907b2</Id> <Status>OK</Status> <ProviderName>Test OAuth NMI</ProviderName> <DateTimeUTC>2021-10-06T21:37:35.6205401Z</DateTimeUTC> <Invoices> <Invoice> <Contact> <ContactID>ea5f94be-d51c-4775-b8cc-fe70539aa915</ContactID> <ContactNumber>9d788d880f299b1eaaa4def1066afc04883126eabbd99574d6</ContactNumber> <ContactStatus>ACTIVE</ContactStatus> <Name>Shawn Fernandes</Name> <FirstName>Shawn</FirstName> <LastName>Fernandes</LastName> <EmailAddress>[email protected]</EmailAddress> <Addresses> <Address> <AddressType>POBOX</AddressType> </Address> <Address> <AddressType>STREET</AddressType> </Address> </Addresses> <Phones> <Phone> <PhoneType>DEFAULT</PhoneType> </Phone> <Phone> <PhoneType>DDI</PhoneType> </Phone> <Phone> <PhoneType>FAX</PhoneType> </Phone> <Phone> <PhoneType>MOBILE</PhoneType> </Phone> </Phones> <UpdatedDateUTC>2021-10-01T01:46:36.037</UpdatedDateUTC> <IsSupplier>false</IsSupplier> <IsCustomer>true</IsCustomer> </Contact> <Date>2021-10-06T00:00:00</Date> <DueDate>2021-10-16T00:00:00</DueDate> <BrandingThemeID>324587a9-7eed-46c0-ad64-fa941a1b5b3e</BrandingThemeID> <Status>AUTHORISED</Status> <LineAmountTypes>Exclusive</LineAmountTypes> <LineItems> <LineItem> <Description>Test Order NMI Gateway</Description> <UnitAmount>93.00</UnitAmount> <TaxType>NONE</TaxType> <TaxAmount>0.00</TaxAmount> <LineAmount>93.00</LineAmount> <AccountCode>220</AccountCode> <Quantity>1.0000</Quantity> <LineItemID>9eff054e-4dd7-4eac-9acd-e3d5ce3277b1</LineItemID> </LineItem> </LineItems> <SubTotal>93.00</SubTotal> <TotalTax>0.00</TotalTax> <Total>93.00</Total> <UpdatedDateUTC>2021-10-06T21:37:35.543</UpdatedDateUTC> <CurrencyCode>USD</CurrencyCode> <Type>ACCREC</Type> <InvoiceID>19f96b3c-05e5-4d2b-88f3-537ee6625931</InvoiceID> <InvoiceNumber>INV-0048</InvoiceNumber> <Reference>Website Design</Reference> <AmountDue>93.00</AmountDue> <AmountPaid>0.00</AmountPaid> <SentToContact>false</SentToContact> <CurrencyRate>1.0000000000</CurrencyRate> <HasErrors>false</HasErrors> </Invoice> </Invoices> </Response>'; 
  101. $resp2 = new SimpleXMLElement($resp2);
  102. var_dump((array)$resp2);
  103.  
  104. echo " ln br9 \n";
  105. echo "ln br10  \n";
  106. $val=(array)$resp2;
  107. var_dump($val['Invoices']);
  108.  
  109. echo " ln br7 \n";
  110. echo "ln br 8 \n";
  111.  
  112. $resp2=json_encode($resp2);
  113. var_dump($resp2);
  114. echo " ln br5 \n";
  115. echo "ln br 6 \n";
  116.  
  117. $json_resp22 = json_decode($resp2,true);
  118.  
  119. var_dump($json_resp22);
  120. echo " ln br1 \n";
  121. echo "ln br 2 \n";
  122. //Parse invoice id from response
  123. $invoice_id2= $json_resp22['Invoices']['Invoice']['InvoiceID'];
  124.  
  125. echo var_dump($invoice_id2);
  126.  
  127.  
File Description
  • XML Parse
  • PHP Code
  • 06 Oct-2021
  • 5.3 Kb
You can Share it: