">
Download presentation
Presentation is loading. Please wait.
1
XML Parsing In PHP
2
What is XML? XML is a language used to represent structured documents or data electronically: Here is an example: <?xml version="1.0" ?> <rooms> <room name="Red"> <capacity>10</capacity> <equipmentList> <equipment>Projector</equipment> </equipmentList> </room> <room name="Green"> <capacity>5</capacity> <equipmentList /> <features> <feature>No Roof</feature> </features> </room> </rooms>
3
What is XML? It is actually a set of syntax rules for creating semantically rich markup languages in a particular domain. In other words you apply XML to create new languages! Any language created via the rules of XML, like MathML, is called an application of XML. So, a key principle of XML is that the markup is separate from content.
4
Well formed and Valid The XML specification defines two levels of conformance for XML documents: well formed and valid. Well-formed documents are mandatory, while validity is optional. A well-formed document compiles with all the WC3 syntax rules of XML – like naming, nesting, and attribute quoting: This guarantees that an XML parser can parse the document without error. A valid XML document references and satisfies a schema: A schema is a separate document whose purpose is to define the legal elements, attributes, and structure of an XML document.
5
Well formed and Valid! You can think of a schema as defining the legal vocabulary, number, and placement of elements and attributes in your markup language. Therefore, a schema defines a particular type or class of documents. The markup language constrains the information to be of a certain type to be considered “legal”.
6
XML is... A flexible format for describing any kind of document.
Like HTML: But you can define whatever tags you want for your application, A self-describing format: An XML document gives complete information about what field values are associated with it, BUT - it just describes a document! Does not say what it means, Does not explain how to display it.
7
XML Applications Computer-computer communications.
Content Management Systems. Wireless Communication Systems. PDAs and Handheld Devices. eLearning and Educational Services. Web Services. Wireless - Internet Integration. And so on…
8
Parts of an XML document
<?xml version "1.0" ?> <CML> <MOL TITLE="Water" > <ATOMS> <ARRAY BUILTIN="ELSYM" > H O H</ARRAY> </ATOMS> <BONDS> <ARRAY BUILTIN="ATID1" >1 2</ARRAY> <ARRAY BUILTIN="ATID2" >2 3</ARRAY> <ARRAY BUILTIN="ORDER" >1 1</ARRAY> </BONDS> </MOL> </CML> Declaration Tags Begin Tags End Tags Attributes Attribute Values
9
To read and update - create and manipulate - an XML document, you will need an XML parser.
There are two basic types of XML parsers: Tree-based parser: This parser transforms an XML document into a tree structure. It analyzes the whole document, and provides access to the tree elements Event-based parser: Views an XML document as a series of events. When a specific event occurs, it calls a function to handle it
10
Parsing XML Response To parse the returned XML we turn to any number of extensions found in PHP. XML extension Basic XML parser based on SAX methodology found in all PHP versions. SimpleXML Arguably the simplest XML parser to use. DOM Maximum flexibility for both parsing and creating XML XMLReader Pull parser, that combines ease of use with high performance.
12
XML Parsing Methodologies
SAX (Simple API for XML) An event based approach where by each action, such “found new tag” needs to be handled. The triggerable events include: open tag close tag tag’s data DOM (Document Object Model) Loads the entire document into memory, creating a “tree” representing the XML data. The tree can then be traversed in multitude of ways.
13
SAX: Simple Example <?php
function startElement($parser, $elementname, $attributes) { print "* Start Element: $elementname \n"; foreach ($attributes as $attname => $attvalue) { print " $attname => $attvalue \n"; } function endElement($parser, $elementname) { print "* End Element: $elementname\n"; function charDataHandler($parser,$data) { if (trim($data) != "") print $data."\n";
14
SAX: Simple Example $parser = xml_parser_create();
/* Disable as case is significant in XML */ xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($parser,"startElement","endElement"); xml_set_character_data_handler($parser, "charDataHandler"); if (($fp = fopen("d:/xml/note.xml", "r"))) { while ($data = fread($fp, 4096)) { xml_parse($parser, $data, feof($fp)); } ?>
15
SAX: Simple Example RESULTS
* Start Element: note * Start Element: to S2 Msc CS * End Element: to * Start Element: from Manjusha * End Element: from * Start Element: heading Reminder * End Element: heading * Start Element: body Lab Exam on April 27! * End Element: body * End Element: note
16
XML DOM According to the DOM, everything in an XML document is a node.
The DOM says: The entire document is a document node Every XML element is an element node The text in the XML elements are text nodes Every attribute is an attribute node Comments are comment nodes
17
initialize the XML parser, load the xml, and output it
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load(“note.xml"); print $xmlDoc->saveXML(); ?> saveXML() function puts the internal XML document into a string, so we can output it
18
Looping through XML file
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load("note.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { print $item->nodeName . " = " . $item->nodeValue . "<br />"; } ?>
19
In the output for the above, you see that there are empty text nodes between each element.
When XML generates, it often contains white-spaces between the nodes. The XML DOM parser treats these as ordinary elements
20
XMLReader Forward moving stream based parser It is a Pull parser
Based on the C# XmlTextReader API Advantages: Low memory footprint Namespace support Simple API Validation support Advanced Feature Set Faster Processing
21
XMLReader: Simple Example xmlreader/reader_simple.xml
<?xml version='1.0'?> <chapter xmlns:a=" xmlns=" <a:title>XMLReader</a:title> <para> First Paragraph </para> <a:section a:id="about"> <title>About this Document</title> <!-- this is a comment --> <?php echo 'Hi! This is PHP version ' . phpversion(); ?> </a:section> </chapter>
22
XMLReader: Simple Example xmlreader/reader_simple.php
$reader = new XMLReader(); $reader->open('reader_simple.xml'); $reader->read(); print "xmlns Attribute value: ".$reader->getAttributeNo(0)."\n\n"; while ($reader->read() && $reader->name != "a:title") { } print "Local Name for Element: ".$reader->localName."\n"; print "Namespace URI for Element: ".$reader->namespaceURI."\n"; while($reader->read()) { switch ($reader->nodeType) { case XMLReader::ELEMENT: print "Element: ".$reader->name."\n"; if ($reader->hasAttributes && $reader->moveToFirstAttribute()) { do { print " ".$reader->name."=".$reader->value."\n"; } while($reader->moveToNextAttribute()); } break; case XMLReader::PI: print "PI Target: ".$reader->name."\n PI Data: ".$reader->value."\n";
23
XMLReader: Simple Example RESULTS
Local Name for Element: title Namespace URI for Element: Element: para Element: a:section a:id=about Element: title PI Target: php PI Data: echo 'Hi! This is PHP version ' . phpversion();
24
Creating XML $dom = new domDocument("1.0","ISO-8859-1");
$dom->formatOutput = 1; $root = $dom->createElement('books'); $branch = $dom->createElement('book'); $branch->setAttribute('ISBN', ' '); $leaf = $dom->createElement('title'); $leaf->appendChild( $dom->createTextNode(‘PHP Guide to Security')); $branch->appendChild($leaf);
25
Creating XML Step 2 $leaf = $dom->createElement('price');
$leaf->appendChild( $dom->createTextNode('32.99')); $branch->appendChild($leaf); $leaf = $dom->createElement('url'); $dom->createCDATASection(‘amazon.com/…’);
26
Creating XML Step 3 $root->appendChild($branch);
$dom->appendChild($root); echo $dom->saveXML();
27
What do we have? <?xml version="1.0" encoding="ISO-8859- 1"?>
<books> <book ISBN=" "> <title>PHP Guide to Security</title> <price>26.37</price> <url><![CDATA[amazon.com/...]]></url> </book> </books>
28
Tree Parsers Pros: Cons:
Full navigation and modification of the XML document Navigating and searching are extremely fast once the tree is loaded into memory Cons: Must wait until entire tree is loaded to begin working with the XML. Memory intensive
29
Streaming Parsers Pros: Cons: Uses minimal memory
Processing takes place immediately while the document is parsed Cons: Minimal to no navigation support (forward only) No document editing capabilities
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.