Skip to content

Instantly share code, notes, and snippets.

@sohelrana820
Last active April 30, 2022 07:30
Show Gist options
  • Save sohelrana820/f722e894064149c2285b3ca5749e8d29 to your computer and use it in GitHub Desktop.
Save sohelrana820/f722e894064149c2285b3ca5749e8d29 to your computer and use it in GitHub Desktop.
Parsing XML with multiple namespaces using PHP
<?php
/**
* @author: Sohel Rana
* @url: https://blog.sohelrana.me/parsing-xml-multiple-namespaces-using-php/
* @license: MIT
* Parsing XML with multiple namespaces using PHP
*/
$xmlString = "<en:Envelope xmlns:en=\"http://www.softcare.com/HIPAA/SC/Enrollment/0301\"
xmlns:sc=\"http://www.softcare.com/HIPAA/SC/0203\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://www.softcare.com/HIPAA/SC/0203 ../schema/types/common-types_0203.xsd http://www.softcare.com/HIPAA/SC/Enrollment/0301 ../schema/Enrollment_0301_rc2.xsd\">
<en:EmployeeDetail>
<en:SubscriberIdentifier>12234432</en:SubscriberIdentifier>
<en:MemberLevelDates>
<en:MaintenanceEffective format=\"CCYYMMDD\">20161202</en:MaintenanceEffective>
<en:EmploymentBegin format=\"CCYYMMDD\">19960422</en:EmploymentBegin>
</en:MemberLevelDates>
<en:MemberDetail>
<en:Person>
<sc:LastName>TEST</sc:LastName>
<sc:FirstName>EE1</sc:FirstName>
<sc:MiddleName>A</sc:MiddleName>
</en:Person>
<en:Identification>
<en:SSN>122332213</en:SSN>
<en:MutuallyDefined>5819144b1defc67d771f172d</en:MutuallyDefined>
</en:Identification>
<en:DemographicInformation>
<en:DateOfBirth format=\"CCYYMMDD\">19520706</en:DateOfBirth>
<en:GenderCode>F</en:GenderCode>
<en:MaritalStatusCode>I</en:MaritalStatusCode>
</en:DemographicInformation>
<en:PaymentInformation>
<sc:AddressLine1>123 street</sc:AddressLine1>
<sc:City>test city</sc:City>
<sc:ProvinceOrStateCode>MA</sc:ProvinceOrStateCode>
<sc:PostalOrZipCode>02571</sc:PostalOrZipCode>
</en:PaymentInformation>
<en:JobTitle>Administrative Support</en:JobTitle>
</en:MemberDetail>
<en:MailingAddress>
<sc:AddressLine1>123 street</sc:AddressLine1>
<sc:City>test city</sc:City>
<sc:ProvinceOrStateCode>MA</sc:ProvinceOrStateCode>
<sc:PostalOrZipCode>02571</sc:PostalOrZipCode>
</en:MailingAddress>
<en:DisabilityInformation/>
</en:EmployeeDetail>
</en:Envelope>";
$xml = simplexml_load_string($xmlString, NULL, NULL, "http://www.softcare.com/HIPAA/SC/Enrollment/0301");
// Getting EmployeeDetail data
var_dump($xml->EmployeeDetail);
// Does't contain any data
var_dump($xml->EmployeeDetail->MemberDetail->Person);
var_dump($xml->EmployeeDetail->MemberDetail->PaymentInformation);
// Getting Person and PaymentInformation data by using children.
$personInfo = $xml->EmployeeDetail->MemberDetail->Person->children('http://www.softcare.com/HIPAA/SC/0203');
$paymentInfo = $xml->EmployeeDetail->MemberDetail->PaymentInformation->children('http://www.softcare.com/HIPAA/SC/0203');
var_dump($personInfo);
var_dump($paymentInfo);
@docetapedro
Copy link

I'm getting error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment