Web Services 从返回的XML字符串获取属性

g6baxovj  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(126)

我尝试获取从CEBroker WebServices调用返回的XML字符串,例如:

<licensees>
    <licensee valid="true" State="FL" licensee_profession="RN" 
        licensee_number="2676612" state_license_format="" first_name="HENRY" last_name="GEITER" ErrorCode="" Message="" TimeStamp="2/18/2022 6:43:20 PM" />
</licensees>

解析字符串以获得属性值,因为它们总是相同的,并且每个字符串只有一个许可证持有者。XML字符串在$xmlresponse中返回,当我使用print_r将其打印出来时,它是正确的,但当我尝试解析时,总是什么也得不到。
我试过了

$xml_string = simplexml_load_string($xmlresponse);
print_r ("this is my xml after going through simplexml_load_string" . $xml_string);

//上面的行不打印$xml_string;

$json = json_encode($xml_string);
            print_r($json);
            $array = json_decode($json,TRUE);
            echo "<p>Array contents are as follows</p>";
            print_r($array);
            var_dump ($array);
            echo "<p>Array contents ended!</p>";

我是XML新手,只需要知道如何解析返回的XML数据或XML文件的节点和属性(在本例中)。
谢谢

nr7wwzry

nr7wwzry1#

如果我没有理解错您的问题,请尝试以下操作,该操作假定有两个许可证持有者的响应:

$xmlresponse = '
<licensees>
   <licensee valid="true" State="FL" licensee_profession="RN" licensee_number="2676612" state_license_format="" first_name="HENRY" last_name="GEITER" ErrorCode="" Message="" TimeStamp="2/18/2022 6:43:20 PM" />
   <licensee valid="false" State="NY" licensee_profession="DC" licensee_number="1234" state_license_format="" first_name="JOHN" last_name="DOE" ErrorCode="" Message="" TimeStamp="1/1/2023 6:43:20 PM" />
</licensees>
';
$xml_string = simplexml_load_string($xmlresponse);
$licensees = $xml_string->xpath("//licensee");

foreach ($licensees as $licensee) {
    foreach ($licensee ->xpath('./@*') as $attribute){
    echo $attribute." ";
    }
    echo "\n";
}

输出量:

true FL RN 2676612  HENRY GEITER   2/18/2022 6:43:20 PM 
false NY DC 1234  JOHN DOE   1/1/2023 6:43:20 PM

输出量

相关问题