Php can';我搞不懂SimpleXML语法
我试图解析soap请求XML日志中的一些项,但似乎不知道如何访问SimpleXMLElement的内部。下面是我正在使用的XML的一个示例:Php can';我搞不懂SimpleXML语法,php,xml,simplexml,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">
<?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>
我更喜欢使用XMLReader: 这是一个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);
}
关于除非您使用的是XPath,否则不需要调用
registerXPathNamespace
。但是,您确实需要遍历SOAPBody
元素,这意味着您需要考虑两个名称空间,而不仅仅是一个名称空间
最好不要依赖名称空间前缀,如ns1
将来保持不变;保证的部分是xmlns
属性中的实际URI。提高可读性的一种方法是为应用程序中使用的XML名称空间定义常量,并在必要时将这些常量传递给->children()
和->attributes()
方法
还请注意,->children()
“选择”一个名称空间,直到另行通知,因此,一旦您选择了Fedex名称空间并遍历到RateRequest
节点,您就可以访问元素,就好像名称空间不是问题一样(因为本文档中下面的所有内容都在同一名称空间中)
下面是一个完整的示例,展示了它的外观()
//为名称空间指定您自己的别名,不要依赖XML始终具有相同的别名
定义('NS_SOAP','http://schemas.xmlsoap.org/soap/envelope/');
定义('NS\U联邦快递费率','http://fedex.com/ws/rate/v9');
//解析XML
$sx=simplexml\u load\u字符串($xml\u字符串);
//$sx现在表示元素
//穿过皂体
$sx\u soap\u body=$sx->children(NS\u soap)->body;
//现在“切换”到Fedex名称空间,并从RateRequest节点开始
$sx_rate_request=$sx_soap_body->children(NS_FEDEX_rate)->rate request;
//现在按正常方式遍历,例如,遍历收件人的国家代码
//(echo不需要使用字符串转换,但这是一个很好的习惯)
echo(字符串)$sx\u rate\u request->requestedshipping->Recipient->Address->CountryCode;
好的,谢谢您的回复,但我并不想学习全新命令的全新语法。我用xmlreader做了一些尝试,虽然它让我可以用我给出的示例xml轻松地获得一些属性,但我还必须解析从fedex返回的响应,其中包含一些重复元素,我真的不想尝试找出另一个xml解析器来获取它们。问题是关于SimpleXML元素-有人吗?不要使用print\u r
或var\u dump
检查SimpleXML对象,它们会完全误导您。请尝试以下功能:
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);
}
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
)
// 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;