我收到優惠提要並希望對其進行處理,以便將其添加到我的數據庫中。下面是它在未來的格式:如何將XML Feed中的數據添加到MySQL數據庫
<deals>
<item>
<couponid>int</couponid>
<merchantid>int</merchantid>
<merchantname>string</merchantname>
<network>string</network>
<label>string</label>
<restrictions>string</restrictions>
<couponcode>string</couponcode>
<link>string</link>
<directlink>string</directlink>
<startdate>YYYY-MM-DD HH:MM TMZ</startdate>
<enddate>YYYY-MM-DD HH:MM TMZ</enddate>
<image>string</image>
<dealtypes>
<type>string</type>
</dealtypes>
<categories>
<category>string</category>
</categories>
<status>status</status>
<lastupdated>YYYY-MM-DD HH:MM TMZ</lastupdated>
<errorreporturl>string</errorreporturl>
<changeaudit>string</changeaudit>
<price>string</price>
<listprice>string</listprice>
<discount>string</discount>
<percent>string</percent>
<local>
<city>string</city>
<state>string</state>
<zipcode>string</zipcode>
<address>string</address>
<businessname>string</businessname>
<businessurl>string</businessurl>
<title2>string</title2>
<expirationdate>string</expirationdate>
<minpurchase>int</minpurchase>
<maxpurchase>int</maxpurchase>
<limitedqty>bool</limitedqty>
<region>string</region>
</local>
</item>
</deals>
我可以使用處理大部分物品:
'couponid' => $node->getElementsByTagName('couponid')->item(0)->nodeValue
但是,我怎麼處理這些「本地」節點內的項目。並非Feed中的所有項目都有此「本地」節點。我將如何處理它?將這項工作:
$localData = $node->getElementsByTagName('local')->item(0);
if $localData {
'city' => $node->getElementsByTagName('local')->item(city)->nodeValue;
'state' => $node->getElementsByTagName('local')->item(state)->nodeValue;
etc..
}
你說過「這會工作嗎」,它工作嗎? 無論如何,也許'$ node-> getElementsByTagName('local') - > getElementsByTagName('city') - > nodeValue'將起作用。 – Dion 2012-01-04 00:03:25