Parsing XML elements with dynamic namespace prefix in PHP
Posted
by
BugKiller
on Stack Overflow
See other posts from Stack Overflow
or by BugKiller
Published on 2013-06-26T10:03:57Z
Indexed on
2013/06/26
10:21 UTC
Read the original article
Hit count: 196
I have the following XML ( you can say SOAP request ) :
<SOAPENV:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:NS="http://xyz.gov/headerschema" >
<SOAPENV:Header>
<NS:myHeader>
<NS:SourceID>223423</NS:SourceID>
</NS:myHeader>
</SOAPENV:Header>
</SOAPENV:Envelope>
I use the following code and it works fine :
<?php
$myRequest ='<SOAPENV:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:NS="http://xyz.gov/headerschema" >
<SOAPENV:Header>
<NS:myHeader>
<NS:SourceID>223423</NS:SourceID>
</NS:myHeader>
</SOAPENV:Header>
</SOAPENV:Envelope>';
$xml = simplexml_load_string($myRequest, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$namespaces = $xml->getNameSpaces(true);
$soapHeader = $xml->children($namespaces['SOAPENV'])->Header;
$myHeader = $soapHeader->children($namespaces['NS'])->myHeader;
echo (string)$myHeader->SourceID;
?>
The Problem
I know the prefix ( SOAPENV + NS ) , but the clients could change the prefix to whatever they want, so they may send me xml document that has ( MY-SOAPENV + MY-NS) prefixes.
My Question
How can I handle this since the namespace prefixes are not static , how can I parse it ?
Thanks
© Stack Overflow or respective owner