无法弄清楚SimpleXML语法

时间:2013-11-20 17:40:28

标签: php xml simplexml

我正在尝试从soap请求XML的日志中解析一些项目,似乎无法弄清楚如何到达SimpleXMLElement的内部。这是我正在使用的XML的一个例子:

<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://fedex.com/ws/rate/v9">
     <SOAP-ENV:Body>
      <ns1:RateRequest>
       <ns1:WebAuthenticationDetail>
        <ns1:UserCredential>
         <ns1:Key>aaaaaaaaaaa</ns1:Key>
         <ns1:Password>aaaaaaaaaaaaaaaaaaa</ns1:Password>
        </ns1:UserCredential>
       </ns1:WebAuthenticationDetail>
       <ns1:ClientDetail>
        <ns1:AccountNumber>11111111</ns1:AccountNumber>
        <ns1:MeterNumber>88888888</ns1:MeterNumber>
       </ns1:ClientDetail>
       <ns1:TransactionDetail>
        <ns1:CustomerTransactionId>1</ns1:CustomerTransactionId>
       </ns1:TransactionDetail>
       <ns1:Version>
        <ns1:ServiceId>crs</ns1:ServiceId>
        <ns1:Major>9</ns1:Major>
        <ns1:Intermediate>0</ns1:Intermediate>
        <ns1:Minor>0</ns1:Minor>
       </ns1:Version>
       <ns1:ReturnTransitAndCommit>true</ns1:ReturnTransitAndCommit>
       <ns1:RequestedShipment>
        <ns1:ShipTimestamp>2013-08-06T12:39:26-04:00</ns1:ShipTimestamp>
        <ns1:DropoffType>REGULAR_PICKUP</ns1:DropoffType>
        <ns1:Shipper>
         <ns1:AccountNumber>11111111</ns1:AccountNumber>
         <ns1:Address>
          <ns1:StreetLines>24 Seaview Blvd</ns1:StreetLines>
          <ns1:City>Port Washington</ns1:City>
          <ns1:StateOrProvinceCode>NY</ns1:StateOrProvinceCode>
          <ns1:PostalCode>11050</ns1:PostalCode>
          <ns1:CountryCode>US</ns1:CountryCode>
         </ns1:Address>
        </ns1:Shipper>
        <ns1:Recipient>
         <ns1:Address>
          <ns1:StreetLines>1234 Fifth Street</ns1:StreetLines>
          <ns1:City>Sixton</ns1:City>
          <ns1:StateOrProvinceCode>AR</ns1:StateOrProvinceCode>
          <ns1:PostalCode>72712</ns1:PostalCode>
          <ns1:CountryCode>US</ns1:CountryCode>
         </ns1:Address>
        </ns1:Recipient>
        <ns1:ShippingChargesPayment>
         <ns1:Payor>
          <ns1:AccountNumber>11111111</ns1:AccountNumber>
          <ns1:CountryCode>US</ns1:CountryCode>
         </ns1:Payor>
        </ns1:ShippingChargesPayment>
        <ns1:RateRequestTypes>ACCOUNT</ns1:RateRequestTypes>
        <ns1:PackageCount>1</ns1:PackageCount>
        <ns1:PackageDetail>INDIVIDUAL_PACKAGES</ns1:PackageDetail>
        <ns1:RequestedPackageLineItems>
         <ns1:Weight>
          <ns1:Units>LB</ns1:Units>
          <ns1:Value>14</ns1:Value>
         </ns1:Weight>
         <ns1:Dimensions>
          <ns1:Length>20</ns1:Length>
          <ns1:Width>20</ns1:Width>
          <ns1:Height>9</ns1:Height>
          <ns1:Units>IN</ns1:Units>
         </ns1:Dimensions>
        </ns1:RequestedPackageLineItems>
       </ns1:RequestedShipment>
      </ns1:RateRequest>
     </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

我正在尝试获取像CountryCode,PostalCode Weight-&gt; Value和所有Dimensions这样的值,但命名空间让我感到困惑。任何时候我解析它,并查看调试器所有我看到的任何给定变量是{SimpleXMLElement} [0]但有些将显示输出 - &gt; asXML()但我的大多数尝试访问数据要么出错,返回false,或返回另一个{SimpleXMLElement} [0]。我只想要这些节点的字符串/值!

以下例如,绝对没有输出:

    $fReq = simplexml_load_string($xmlRequest);
    $fReq->registerXPathNamespace('ns1', 'http://fedex.com/ws/rate/v9');
    $ns = $fReq->getNameSpaces(true);
    $rr = $fReq->children($ns['ns1']);
    echo $rr->asXML()."\n";

2 个答案:

答案 0 :(得分:0)

我更喜欢使用XMLReader:http://us3.php.net/manual/en/class.xmlreader.php

这是PHP的一个示例,只需很少的修改即可满足您的要求:

function xmlParse($content,$wrapperName,$callback,$limit=NULL){
    $xml = new XMLReader();
    $xml->xml($content);

    $n=0;
    $x=0;
    while($xml->read()){
        if($xml->nodeType==XMLReader::ELEMENT && $xml->name == $wrapperName){
            while($xml->read() && $xml->name != $wrapperName){
                if($xml->nodeType==XMLReader::ELEMENT){
                    $name = $xml->name;
                    $xml->read();
                    $value = $xml->value;
                    if(preg_match("/[^\s]/",$value)){
                        $subarray[$name] = $value;
                    }
                }
            }
            if($limit==NULL || $x<$limit){
                if($callback($subarray)){
                    $x++;
                }
                unset($subarray);
            }
            $n++;
        }
    }
    $xml->close();
}

xmlParse($test,'SOAP-ENV:Envelope','callback');

function callback($array){
    var_dump($array);

}

只需用XML执行此操作并返回

Array
(
    [ns1:Key] => aaaaaaaaaaa
    [ns1:Password] => aaaaaaaaaaaaaaaaaaa
    [ns1:AccountNumber] => 11111111
    [ns1:MeterNumber] => 88888888
    [ns1:CustomerTransactionId] => 1
    [ns1:ServiceId] => crs
    [ns1:Major] => 9
    [ns1:Intermediate] => 0
    [ns1:Minor] => 0
    [ns1:ReturnTransitAndCommit] => true
    [ns1:ShipTimestamp] => 2013-08-06T12:39:26-04:00
    [ns1:DropoffType] => REGULAR_PICKUP
    [ns1:StreetLines] => 1234 Fifth Street
    [ns1:City] => Sixton
    [ns1:StateOrProvinceCode] => AR
    [ns1:PostalCode] => 72712
    [ns1:CountryCode] => US
    [ns1:RateRequestTypes] => ACCOUNT
    [ns1:PackageCount] => 1
    [ns1:PackageDetail] => INDIVIDUAL_PACKAGES
    [ns1:Units] => IN
    [ns1:Value] => 14
    [ns1:Length] => 20
    [ns1:Width] => 20
    [ns1:Height] => 9
)

此致

答案 1 :(得分:0)

除非您使用XPath,否则无需致电registerXPathNamespace。但是,您需要遍历SOAP Body元素,这意味着您需要考虑两个名称空间,而不仅仅是一个。

最好不要依赖像ns1之类的名称空间前缀,以及将来保持不变;保证的部分是xmlns属性中的实际URI。使其更具可读性的一种方法是为应用程序中使用的XML命名空间定义常量,并在必要时将这些常量传递给->children()->attributes()方法。

另请注意->children()&#34;选择&#34;命名空间直到另行通知,因此一旦您选择了Fedex命名空间并遍历到RateRequest节点,您就可以访问元素,就像命名空间不是问题一样(因为在此之下的所有内容都是如此)文档位于同一名称空间中。

这是一个完整的示例,展示了它的外观(see a live demo here)。

// Give your own aliases to namespaces, don't rely on the XML always having the same ones
define('NS_SOAP', 'http://schemas.xmlsoap.org/soap/envelope/');
define('NS_FEDEX_RATE', 'http://fedex.com/ws/rate/v9');

// Parse the XML
$sx = simplexml_load_string($xml_string);
// $sx now represents the <SOAP-ENV:Envelope> element

// Traverse through the SOAP Body
$sx_soap_body = $sx->children(NS_SOAP)->Body;

// Now "switch" to the Fedex namespace, and start with the RateRequest node
$sx_rate_request = $sx_soap_body->children(NS_FEDEX_RATE)->RateRequest;

// Now traverse as normal, e.g. to the Recipient's CountryCode
// (the string cast isn't necessary for echo, but is a good habit to get into)
echo (string)$sx_rate_request->RequestedShipment->Recipient->Address->CountryCode;