一般解析XML和XML的新手......我正在研究一个例子,但有一些我无法弄清楚的事情:
我目前正在使用诸如此类的内容来表达价值观:
$order->reference = $doc->getElementsByTagName("reference")->item(0)->nodeValue;
我意识到获得x的第一次出现的值并且它工作正常。
现在,由于customer
和purchaser
内有相同的值,我如何定位其中一个?每个最多只有一个实例。
另外,我希望<orderItem>
中有多个<orderItems>
...我怎样才能遍历这些...最好创建类似于数组的东西,因为我不知道会有多少。< / p>
示例xml:
<order>
<reference/>
<status>open | request | requested | acceptance | accepted | fulfillment | fulfilled | completion | completed | canceled | failed</status>
<statusChanged>2010-08-15T00:00:00.000Z</statusChanged>
<test>true | false</test>
<due>2010-08-15T00:00:00.000Z</due>
<returnStatus>none | partial | full</returnStatus>
<currency/>
<referrer/>
<originIp/>
<total>0.0</total>
<tax>0.0</tax>
<shipping>0.0</shipping>
<sourceName/>
<sourceKey/>
<sourceCampaign/>
<customer>
<firstName/>
<lastName/>
<company/>
<email/>
<phoneNumber/>
</customer>
<purchaser>
<firstName/>
<lastName/>
<company/>
<email/>
<phoneNumber/>
</purchaser>
<address>
<addressLine1/>
<addressLine2/>
<city/>
<region/>
<regionCustom/>
<postalCode/>
<country/>
</address>
<orderItems>
<orderItem>
<productDisplay/>
<productName/>
<quantity>0</quantity>
<subscriptionReference/>
</orderItem>
</orderItems>
<payments>
<payment>
<status>open | request | requested | acceptance | accepted | fulfillment | fulfilled | completion | completed | canceled | failed</status>
<statusChanged>2010-08-15T00:00:00.000Z</statusChanged>
<methodType>paypal | creditcard | test | bank | check | purchase-order | free</methodType>
<declinedReason>internal-error | unsupported-country | expired-card | declined | risk | processor-risk | connection | unknown | cc-address-verification | cc-cvv | voice-auth</declinedReason>
<currency/>
<total>0.0</total>
</payment>
</payments>
</order>
编辑:
没有错误但没有客户/购买者的价值。
private function parseFsprgOrder($doc) {
//show values coming in
echo '<pre>',print_r($doc, 1),'</pre>';
$order = new FsprgOrder();
$order->reference = $doc->getElementsByTagName("reference")->item(0)->nodeValue;
$order->status = $doc->getElementsByTagName("status")->item(0)->nodeValue;
$order->statusChanged = strtotime($doc->getElementsByTagName("statusChanged")->item(0)->nodeValue);
$order->test = $doc->getElementsByTagName("test")->item(0)->nodeValue;
$order->due = strtotime($doc->getElementsByTagName("due")->item(0)->nodeValue);
$order->returnStatus = $doc->getElementsByTagName("returnStatus")->item(0)->nodeValue;
$order->currency = $doc->getElementsByTagName("currency")->item(0)->nodeValue;
$order->referrer = $doc->getElementsByTagName("referrer")->item(0)->nodeValue;
$order->originIp = $doc->getElementsByTagName("originIp")->item(0)->nodeValue;
$order->total = $doc->getElementsByTagName("total")->item(0)->nodeValue;
$order->tax = $doc->getElementsByTagName("tax")->item(0)->nodeValue;
$order->shipping = $doc->getElementsByTagName("shipping")->item(0)->nodeValue;
$order->sourceName = $doc->getElementsByTagName("sourceName")->item(0)->nodeValue;
$order->sourceKey = $doc->getElementsByTagName("sourceKey")->item(0)->nodeValue;
$order->sourceCampaign = $doc->getElementsByTagName("sourceCampaign")->item(0)->nodeValue;
//customer info
$customerNodes = $doc->getElementsByTagName("customer")->item(0);
if($customerNodes->length > 0)
{
$customer = new FsprgCustomer();
$customer->firstName = $customerNodes->getElementsByTagName("firstName")->item(0)->nodeValue;
$customer->lastName = $customerNodes->getElementsByTagName("lastName")->item(0)->nodeValue;
$customer->company = $customerNodes->getElementsByTagName("company")->item(0)->nodeValue;
$customer->email = $customerNodes->getElementsByTagName("email")->item(0)->nodeValue;
$customer->phoneNumber = $customerNodes->getElementsByTagName("phoneNumber")->item(0)->nodeValue;
$order->customer = $customer;
}
//purchaser info
$purchaserNodes = $doc->getElementsByTagName("purchaser")->item(0);
if($purchaserNodes->length > 0)
{
$purchaser = new FsprgPurchaser();
$purchaser->firstName = $purchaserNodes->getElementsByTagName("firstName")->item(0)->nodeValue;
$purchaser->lastName = $purchaserNodes->getElementsByTagName("lastName")->item(0)->nodeValue;
$purchaser->company = $purchaserNodes->getElementsByTagName("company")->item(0)->nodeValue;
$purchaser->email = $purchaserNodes->getElementsByTagName("email")->item(0)->nodeValue;
$purchaser->phoneNumber = $purchaserNodes->getElementsByTagName("phoneNumber")->item(0)->nodeValue;
$order->purchaser = $purchaser;
}
return $order;
}
class FsprgCustomer {
public $firstName;
public $lastName;
public $company;
public $email;
public $phoneNumber;
}
class FsprgPurchaser {
public $firstName;
public $lastName;
public $company;
public $email;
public $phoneNumber;
}
class FsprgOrder {
public $reference;
public $status;
public $statusChanged;
public $test;
public $due;
public $returnStatus;
public $currency;
public $referrer;
public $originIp;
public $total;
public $tax;
public $shipping;
public $sourceName;
public $sourceKey;
public $sourceCampaign;
}
答案 0 :(得分:2)
$customerNodes=order->getElemensByTagName('customer');
if($customerNodes->length>0)
$customer=$customerNodes->item(0);
$purchaseNodes=order->getElemensByTagName('purchase');
if($purchaseNodes->length>0)
$purchase=$purchaseNodes->item(0);
$orderItemNodes=order->getElemensByTagName('orderItem');
if($orderItemNodes->length>0)
foreach($orderItemNodes as $orderItemNode){
//process $orderItemNode
}
答案 1 :(得分:0)
检查DOMXPath::evaluate()。这允许您使用XPath表达式从DOM中获取值。
获取节点列表:
$xpath - new DOMXPath($domDocument);
$nodeList = $xpath->evaluate('/order');
或标量值:
$xpath - new DOMXPath($domDocument);
$string = $xpath->evaluate('string(/order/customer/firstName)');
答案 2 :(得分:0)
//customer info
$customerNodes = $doc->getElementsByTagName("customer");
if($customerNodes->length > 0)
{
$customer = new FsprgCustomer();
$customer->firstName = $customerNodes->item(0)->getElementsByTagName("firstName")->item(0)->nodeValue;
$customer->lastName = $customerNodes->item(0)->getElementsByTagName("lastName")->item(0)->nodeValue;
$customer->company = $customerNodes->item(0)->getElementsByTagName("company")->item(0)->nodeValue;
$customer->email = $customerNodes->item(0)->getElementsByTagName("email")->item(0)->nodeValue;
$customer->phoneNumber = $customerNodes->item(0)->getElementsByTagName("phoneNumber")->item(0)->nodeValue;
$order->customer = $customer;
}