Question

I get response from SOAP server which has zero or more transactions of different types in each response.

Each transaction type is extension of base transaction type.

Different transaction types are processed differently.

Is there a way in PHP to get transaction type for each of transactions in response (other then trying to figure difference in elements within each complex type)?

There is lot of types and lot of elements in each type....

Is there any class which could get this?

Following is just illustration...

<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1">
  <id>24111</id><something>00000000</something><name>Blah</name>
</transactions>
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8">
  <id>24111</id><somethingelse>011</somethingelse>
</transactions>
Was it helpful?

Solution

I 'm not quite sure if this answer fits your question exactly. The following code snippet gets the type attribute value by their given namespaces and not the type of the namespaced value itself.

Done with PHP 's own Document Object Model.

<?php
$str = <<<XML
    <content>
        <transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1">
            <id>24111</id>
            <something>00000000</something>
            <name>Blah</name>
        </transactions>
        <transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8">
            <id>24111</id>
            <somethingelse>011</somethingelse>
        </transactions>
    </content>
XML;

$doc = new DomDocument();
$doc->loadXML($str);

$nodeList = $doc->getElementsByTagName('transactions');
foreach ($nodeList as $element) {
    $value = $element->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'type');
    echo $value . "\n";
}

This will output the two given types "ns2:type1" and "ns2:type8".

OTHER TIPS

I can parse your elements with simple_html_dom.

Here is the link for it.

An example is here :

<?php
include "simple_html_dom.php";
$html_nb = '
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1"><id>24111</id><something>00000000</something><name>Blah</name>
</transactions>
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8"><id>24111</id><somethingelse>011</somethingelse>
</transactions>';
function chtml($str){
    if(strpos("<html>", $str) !== false)
        return '<html><whole_code>'.$str.'</whole_code></html>';
    else
        return "<whole_code>".$str."</whole_code>";
}
function find_element_type($str){
    if(preg_match_all("/\<(.*?)\>/i", $str, $matches))
        return $matches[1][0];
    else
        return false;
}
function get_xsi_type($str){
    if(preg_match_all("/xsi\:type\=\"(.*?)\"/i", $str, $matches))
        return $matches[1][0];
    else
        return false;
}

$html = new simple_html_dom();
$html_2 = new simple_html_dom();
$html->load(chtml($html_nb));
$max_type = 10;

$element = $html->find('whole_code');
$e = $element[0]->innertext;
$html_2->load(chtml($e));
$k = 0;
while($html_2->find("whole_code",false)->children($k) != "")
{
   $all = $html_2->find("whole_code",false)->children($k);
   echo get_xsi_type($all) . "<br>";
   echo find_element_type($all) . " : " .$all."<br>";
   $k++;
}
echo "<hr>";

The result :

ns2:type1
transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1" : 2411100000000Blah 
ns2:type8
transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8" : 24111011 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top