Presentation is loading. Please wait.

Presentation is loading. Please wait.

CG0119 Web Database Systems Parsing XML using SimpleXML.

Similar presentations


Presentation on theme: "CG0119 Web Database Systems Parsing XML using SimpleXML."— Presentation transcript:

1 CG0119 Web Database Systems Parsing XML using SimpleXML

2 SimpleXML What is it? –A built in php library for manipulating xml files using an O-O approach –Allows you to read, write and query xml files. –A very simple interface hiding complex functionality.

3 SimpleXML Example – XML Document D Distinction M Merit P Pass F Fail grade.xml... SimpleXML represents this hierachical tree structure as a set of objects that ‘own’ or contain other objects. If we wanted to display all the grades we could iterate just through those elements like this: In psuedocode foreach grades->grade as currentgrade show currentgrade->gradeID show currentgrade->result end foreach documentdocument->grades document->grades->gradedocument->grades->grade->gradeID

4 Example – XML Document The aim of the example used in these slides is to list the grade data in a XHTML table e.g. FailF …… DistinctionD ResultID

5 SimpleXML Example – PHP Code Steps involved: 1.Create an instance of a simpleXMLElement class and load the XML file 2.Parse the XML by navigating through the document one node at a time REMEMBER: you are navigating a tree structure which is a hierarchy of parent/child nesting though SimpleXML represents this in O-O terms.

6 Step 1. Create simpleXML instance & load file and create XML // create a SimpleXML instance // two ways: // by passing a string to the constructor $xmlString = “…a whole xml document string”; $xml = new SimpleXMLElement($xmlString); // or by explicitly loading a file... $grades = simplexml_load_file(‘./grade.xml'); From now in the workshops we’ll only use the second method, explicitly loading a file.

7 Step 2. Navigate through the document foreach ($grades->grade as $currentgrade)‏ { $id = $currentgrade->gradeID; $result $currentgrade->result; echo “ $id $result \n”; } OR… foreach ($grades->grade as $currentgrade)‏ { echo “ {$currentgrade->gradeID} {$currentgrade->result} \n”; } simpleXML implements an iterator interface which means we can move through our xml document as though it were any other class object or an array. In this example we’re reading through each set of grade nodes assigning them to the variable $currentgrade. Copy the contents of the gradeID that’s in $currentgrade into a simple variable, just so that it’s easier to embed in a string Don’t copy into a simple variable but instead put the object method call in braces.

8 Summary SimpleXML is an O-O approach –More features for XML support than SAX (e.g. querying data with xpath, simple O-O interface, can add and subtract elements from the xml document tree.)‏ Use the references on the next page. They are good resources

9 Recommended Reading http://www.php.net/manual/en/ref.simplexml.php - PHP Manual: SimpleXML Functions Although a search on Google for simpleXML tutorial will give you lots of other sources.


Download ppt "CG0119 Web Database Systems Parsing XML using SimpleXML."

Similar presentations


Ads by Google