I have the following feed which I wish to parse and grab certain data from
http://xmlfeeds.centrebet.com/xmlRugbyLeaguefeed.xml
Whilst I was able to do this in past using a class to pull the XML into an array. I am suffering a few complications with it now, and there have been changes almost weekly that have made it hard to automate the grabbing as I was basing it on specific keywords.
What I want to grab from the XML is only the master events that have the attribute of
TopLevelName=”NRL Round 18″ (this will obviously change each week to round 19, 20 etc)
I then only need to get the following for each of the events under that masterevent
- “straight bet” “price” for each “Competitor/competitorname”
- EventURL
- competitorname
I ave scrapped my code as it was overly complex but can paste it if you like, I was using this XML parser
http://www.bin-co.com/php/scripts/xml2array/
You can do this very easy with SimpleXML, XPath, and for-each loops.
There are only a few things to keep in mind with SimpleXML objects:
SimpleXMLElementSimpleXMLElementwith array-notation (e.g.,Element['attributeName'])Element->ChildElementsorElement->{Child-Element-With-Strange-Name})(string) Elementor(string) Element['attribute'])xpathmethod.childrenmethod’s first argument.In general, whenever you have data-structured (vs document-structured) XML of a moderate size, the path of least resistance is
SimpleXML. If you have a very large document, use a combination ofXMLReaderto break the document up into chunks, andXMLReader::expand()to process those chunks usingDOMDocumentorSimpleXML.The following function will extract the data you want into a structured array:
From here it is a simple matter to insert this data into a database (code below is untested):