parse an XML with SimpleXML which has multiple namespaces
I think you need to register the namespacing and access with XPath. Something like the following should get you going (I haven't the facility to test this).
$xml = simplexml_load_string($res, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('eb', 'http://www.ebxml.org/namespaces/messageHeader');
$xml->registerXPathNamespace('wsse', 'http://schemas.xmlsoap.org/ws/2002/12/secext');
Then you can do something like:
foreach($xml->xpath('//eb:MessageHeader') as $header)
{
var_export($header->xpath('//eb:CPAId')); // Should output 'something'.
}
You may not need to register the namespacing, thinking about it, as they are alredy present in the XML. Not sure on this though, would need to test.
Hope this helps.
Parsing XML with multiple namespaces
The poorly documented fact in play here is that when you select a namespace with ->children
, it remains in effect for descendent nodes.
So when you ask for $sxe->children("soapenv", true)->Body->requestContactResponse
, SimpleXML assumes you are still talking about the "soapenv"
namespace, so is looking for the element <soapenv:requestContactResponse>
, which doesn't exist.
To switch back to the default namespace, you need to call ->children
again, with a NULL
namespace:
$sx->children("soapenv", true)->Body->children(NULL)->requestContactResponse->requestContactReturn->id
Parse XML with multiple namespace to JSON - PHP
I think your getting the two namespaces mixed up, your trying to register the soapenv
prefix with the URI used for ser
. Not that this affects the code, but it may not give what your expecting.
As for not knowing the namespaces in advance, in SimpleXML you can use getDocNamespaces()
to fetch the namespaces from the document and then loop through and register them against the prefixes. So the following code fetches the XML content of the Body...
$xml_str = str_replace(PHP_EOL, '', $xmlstr);
$xml = simplexml_load_string($xml_str,'SimpleXMLElement', LIBXML_NOCDATA);
$ns = $xml->getDocNamespaces(true);
foreach ( $ns as $prefix => $URI ) {
$xml->registerXPathNamespace($prefix, $URI);
}
$j_obj = json_encode($xml->xpath('//soapenv:Body/*'));
Parsing XML with multiple namespaces in PHP
I just want to post with an answer to this awful question. Sorry.
Namespaces are irrelavent with DOM - I just wasn't getting the nodeValue from the Element.
$doc = new DOMDocument();
$doc->load($url);
$feed = $doc->getElementsByTagName("entry");
foreach($feed as $entry) {
$id = $entry->getElementsByTagName("id")->item(0)->nodeValue;
echo $id;
$id = $entry->getElementsByTagName("othervalue")->item(0)->nodeValue;
echo $othervalue;
}
Parse XML namespaces with php SimpleXML
I have given same type of answer here - solution to your question
You just need to register Namespace and then you can work normally with simplexml_load_file and XPath
<?php
$data = "http://alerts.weather.gov/cap/tx.php?x=1";
$entries = file_get_contents($data);
$entries = new SimpleXmlElement($entries);
if(count($entries)):
//echo "<pre>";print_r($entries);die;
//alternate way other than registring NameSpace
//$asin = $asins->xpath("//*[local-name() = 'ASIN']");
$entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $entries->xpath("//prefix:entry");
//echo count($asin);
//echo "<pre>";print_r($result);die;
foreach ($result as $entry):
//echo "<pre>";print_r($entry);die;
$dc = $entry->children('urn:oasis:names:tc:emergency:cap:1.1');
echo $dc->event."<br/>";
echo $dc->effective."<br/>";
echo "<hr>";
endforeach;
endif;
That's it.
Related Topics
How to PHP-Unserialize a Jquery-Serialized Form
PHP Library For Parsing Xml With a Colons in Tag Names
Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error
Notice: Trying to Get Property of Non-Object Error
Find Highest Value in Multidimensional Array
Is PHP Compiled or Interpreted
How to Give PHP Write Access to a Directory
How to Hide .PHP Extension in .Htaccess
Paypal Access - Ssl Certificate: Unable to Get Local Issuer Certificate
Making Sure PHP Substr Finishes on a Word Not a Character
Execute PHP Scripts Within Node.Js Web Server
How to Find Out How "Deep" a PHP Array Is
How to Write a File in Utf-8 Format
Connect PHP to Mssql Via Pdo Odbc
Difference Between a Language Construct and a "Built-In" Function in PHP