1

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..
}
1
  • You said "Would this work", does it work? Anyway maybe $node->getElementsByTagName('local')->getElementsByTagName('city')->nodeValue would work.. Commented Jan 4, 2012 at 0:03

2 Answers 2

1

As per DOMNodeListdocs, 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.

Sign up to request clarification or add additional context in comments.

1 Comment

The original feed parsing script was provided by the feed owner. Not sure why they didn't use SimpleXML. The new script I'm writing is based on the code you gave me. Thanks so much!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.