I'm getting a deals feed and want to process it so I can add it my DB. Here is the format it is coming in:
<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>
I can process most of the items using:
'couponid' => $node->getElementsByTagName('couponid')->item(0)->nodeValue
However, how do I process the items within the "local" node. Not all items in the feed have this "local" node. How would I process it? Would this work:
$localData = $node->getElementsByTagName('local')->item(0);
if $localData {
'city' => $node->getElementsByTagName('local')->item(city)->nodeValue;
'state' => $node->getElementsByTagName('local')->item(state)->nodeValue;
etc..
}
As per DOMNodeList
docs , DOMNodeList::item
will return
The node at the indexth position in the DOMNodeList, or NULL if that is not a valid index.
So, no, you can't do $node->getElementsByTagName('local')->item(city)
.
When you do this $localData = $node->getElementsByTagName('local')->item(0);
your $localData
variable is set to a DOMNode
object, whose children you can then access like any other DOMNode
object's children.
However :
If you're simply reading XML and not writing/appending, PHP's SimpleXML is much simpler to use (hence the name) than DOM and I'd recommend it in your case:
$deals = new SimpleXMLElement($my_xml);
$city = $deals->item->local->city[0];
$state = (string) $deals->item->local->state;
echo "city: $city\n";
echo "state: $state\n";
Note that the method using the (string)
cast yields the same result as simply referencing the [0]
key of the element.
In php you can do the following:
-> Get the xml file :
$myXMLString = file_get_contents($url);
$doc = new DOMDocument('1.0', 'iso-8859-1');
$doc->loadXML($myXMLString);
$deals = $doc->getElementsByTagName("item");
foreach($deals as $item){
functionDeals($item);
}
Then in function "functionDeals" extract the sub element the same way. Otherwise the esiest way is to use Hibernate (Java): follow this link http://javaboutique.internet.com/tutorials/mapping/
Hope this help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.