从带有SimpleXML的PHP SOAP响应解析xml数据的最有效方法


Most efficient way to parse xml data from PHP SOAP response w/ SimpleXML?

我有一个XML数据集,我通过SOAP和PHP使用它。我正试图从xml中解析出这些值,以便使用这些值填充到数据库中。我已经四处寻找了一段时间,想找出最好的方法。以下是一些xml的示例:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
<PaymentNotification xmlns="http://apilistener.envoyservices.com">
  <payment>
    <uniqueReference>ESDEUR11039872</uniqueReference>      
    <epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
    <postingDate>2010-11-15T15:19:45</postingDate>
    <bankCurrency>EUR</bankCurrency>
    <bankAmount>1.00</bankAmount>
    <appliedCurrency>EUR</appliedCurrency>
    <appliedAmount>1.00</appliedAmount>
    <countryCode>ES</countryCode>
    <bankInformation>Sean Wood</bankInformation>
  <merchantReference>ESDEUR11039872</merchantReference>
   </payment>
    </PaymentNotification>
  </soap:Body>
</soap:Envelope>

使用SimpleXMl和namespaces/xpath,我可以访问标记和其中的值。然而,我似乎能够解析出值的唯一方法是循环遍历节点中的每个元素,检查它的名称,如果它与我的字符串比较匹配,则将其分配给数组变量。我必须分析出多次付款。例如:

//all payments
foreach($data->children() as $child) {
   //one payment
   foreach($child->children() as $subChild) {
     switch($subChild->getName()) {
        case "bankCurrency":
           $newPayment['bankCurrency'] = (string)$subChild;
       break;
    case "bankPayment":
       $newPayment['bankAmount'] = (string)$subChild;
       break;
    }
  }
}

这是最好的方法吗?还是我完全错过了其他的路?谢谢

经过多次尝试和错误,我意识到我实际上可以更容易地访问xml元素。在初始循环中,我可以使用$child->bankCurrency直接访问元素。不需要进入内部循环并进行任何解析。我终于开始思考这个PHP xml的东西了。