parse a SOAP XML response with Namespaces using PHP

后端 未结 1 1678
时光说笑
时光说笑 2021-01-21 14:59

I have this ugly XML which has alot of namespaces on it, when I try to load it with simpleXML if i indicate the first namespace I\'d get an xml object ,but following tags with o

相关标签:
1条回答
  • 2021-01-21 15:20

    The rate data can be accessed like this:

    Demo

    $obj = simplexml_load_string($xml);
    
    foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
    {
        echo (string)$rate->RateName . "\n";
    }
    

    Outputs

    Rack Rate
    Normal Rate

    If you want the Rate attributes, you can get them like this (within the loop):

    echo $rate->attributes()->RateID;
    

    You can read the R1 and R2 elements like this:

    foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
    {
        if(isset($rate->R1))
        {
           echo $rate->R1->attributes()->Name;
           echo $rate->R1->attributes()->MinRate;
        }
        if(isset($rate->R2))
        {
           echo $rate->R2->attributes()->Name;
           echo $rate->R2->attributes()->MinRate;
        }
    }
    
    0 讨论(0)
提交回复
热议问题