UFCEWT20 3 Advanced Topics in Web Development 201213
UFCEWT-20 -3 Advanced Topics in Web Development 2012/13 Lecture 3 : PHP 5 -DOM API
DOM : recap An object-based, language-neutral, application programming interface (API) for XML and HTML documents - allows programs and scripts to build documents, navigate their structure, add, modify or delete elements and content - provides a foundation for developing querying, filtering, transformation, rendering etc. applications on top of DOM implementations In contrast to “Serial Access XML” (sax) a good way to think of the DOM is as “Directly Obtainable in Memory” objects representing the nodes, attributes and content of documents
XML document structure (recap) root element; every well formed xml document must be enclosed by exactly one root element. a comment; comments must be delimited by the <!- --> characters as in xhtml empty elements <? xml version="1. 0" encoding="UTF-8"? > <patient nhs-no="7503557856"> <!-- Patient demographics --> <name > <first>Joseph</first> <middle>Michael</middle> <last>Bloggs</last> <previous/> <preferred>Joe</preferred> </name> <title>Mr</title> <address> <street>2 Gloucester Road</street> <street /> <city>Bristol</city> <county>Avon</county> <postcode>BS 2 4 QS</postcode> </address> <tel> <home>0117 9541054</home> <mobile>07710 234674</mobile> </tel> <email>joe. bloggs@email. com</email> <fax /> </patient> xml declaration (optional) used by xml processor; this documents conforms to xml version 1 and uses the UTF-8 standard (Unicode optimized for ASCII) attribute; attributes provide additional information about an element and consist of a name value pair; the value must be enclosed in a single (‘) or double quote (“) a simple element containing text a complex element containing other elements and text
PHP 5 DOM Outline o Create DOM Object o Load XML from a file/string o Manipulate DOM Object o Search/Delete/Create/Replace DOMNode o Set/Get DOMAttribute o Save DOM Object into a XML file.
Load XML from a file o Syntax DOMDocument: : load ( string $filename [, int $options = 0 ] ) • Example <? php $dom = new DOMDocument("1. 0", // load XML from a book. xml $dom->load(" book. xml "); echo $dom->save. XML(); ? > "utf-8");
Load XML from a string o Syntax DOMDocument: : load. XML ( string $source [, int $options = 0 ] ) o Example <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load. XML("<? xml version='1. 0' encoding='utf-8'? >"); echo $dom->save. XML(); ? >
Search DOMNode o Syntax DOMNode. List DOMElement: : get. Elements. By. Tag. Name ( string $name ) DOMNode. List DOMXPath: : query ( string $expression [, DOMNode $contextnode ] )
Using XPath to search documents Sometimes there is a need to search through a document (e. g. when the exact format or order of the nodes is not known in advance) or when only a small set of nodes need to be found in a very large document. The basic format of a XPath query takes the form “a/b/c” where a, b, c are nested xml tags of the form <a><b><c/></b></a>. Some common XPath queries are as follows: a : matches any tag named a a/b : matches any tag named b, directly contained in the tag a a/b/. . : matches and returns the tag a instead of b a//b : matches b when it is any descendent of a a[31] : matches the 31 st a tag a[last()] : matches the very last a tag a[@att] : matches any a with an attribute named “att” a[@att=“val”] matches any tag called a with an attribute named “att” with the value “val”
example file (book. xml) <? xml version="1. 0" encoding="utf-8"? > <books> <book sn="0001"> <auth>Dai</auth> <name>Practical Programming in Tcl and Tk</name> <price>£ 30. 00</price> </book> <book sn="0002"> <auth>Laby</auth> <name>Programming PHP</name> <price>£ 20. 00</price> </book> <book sn="0003"> <auth>Hill</auth> <name>Thinking in Java</name> <price>£ 15. 00</price> </books>
Example for DOMElement: : get. Elements. By. Tag. Name <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load("book. xml"); $nodes = $dom->get. Elements. By. Tag. Name("book"); foreach($nodes as $n) { echo $n->get. Attribute("sn"). " "; } ? > o Output 0001 0002 0003
Example for DOMXPath: : query <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load("book. xml"); $xpath = new DOMXPath($dom); $nodes = $xpath->query("/books/book[@sn='0002']"); foreach ($nodes as $n) { $auth. Nodes = $n->get. Elements. By. Tag. Name("auth"); foreach ($auth. Nodes as $n 2) { echo $n 2 ->node. Name. " = ". $n 2 ->node. Value; } <book sn="0002"> } <auth>Laby</auth> ? > o Output Auth = Laby <name>Programming PHP</name> <price>£ 20. 00</price> </book>
Example: Delete DOMNode <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load("book. xml"); $nodes = $dom->get. Elements. By. Tag. Name("book"); foreach ($nodes as $n) { if($n->get. Attribute("sn") == "0003") { $parent = $n->parent. Node; $parent->remove. Child($n); <? xml version="1. 0" encoding="utf-8"? > } Output <books> } <book sn="0001"> <auth>Dai</auth> echo $dom->save. XML(); <name>Practical Programming in Tcl and Tk</name> <price>£ 30. 00</price> ? > </book> <book sn="0002"> <auth>Laby</auth> <name>Programming PHP</name> <price>£ 20. 00</price> </books>
Example: Create DOMNode <? php $dom = new DOMDocument('1. 0', 'utf-8'); $node = $dom>create. Element('test', 'This is the root element!'); $dom->append. Child($node); echo $dom->save. XML(); ? > • Output <? xml version="1. 0" encoding="utf-8"? > <test>This is the root element!</test>
Example: Create DOMNode <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load("book. xml"); $nodes = $dom->get. Elements. By. Tag. Name("book"); foreach ($nodes as $n) { if($n->get. Attribute("sn") == "0001") { $tag = $dom->create. Element(‘tag', 'Good'); $n->append. Child($tag); } } <book sn="0001"> Result <auth>Dai</auth> echo $dom->save. XML(); ? > <name>Practical Programming in Tcl and Tk</name> <price>£ 30. 00</price> <tag>Good</tag> </book>
Example: Replace DOMNode <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load("book. xml"); $nodes = $dom->get. Elements. By. Tag. Name("book"); foreach ($nodes as $n) { if($n->get. Attribute("sn") == "0001") { $new. Price = $dom->create. Element('price', ‘£ 40. 00'); $old. Price = $n->get. Elements. By. Tag. Name("price")->item(0); $n->replace. Child($new. Price, $old. Price); } } echo $dom->save. XML(); ? > <book sn="0001"> Result <auth>Dai</auth> <name>Practical Programming in Tcl and Tk</name> <price>£ 40. 00</price> </book>
Example: Get/Set DOMAttribute <? php $dom = new DOMDocument("1. 0", "utf-8"); $dom->load("book. xml"); $nodes = $dom->get. Elements. By. Tag. Name("book"); foreach ($nodes as $n) { $sn = $n->get. Attribute("sn"); $sn = "SN-". $sn; $n->set. Attribute("sn", $sn); } echo $dom->save. XML(); ? > <book sn="0001"> … </book> <book sn="0002"> … </book> <book sn=“SN-0001"> … </book> <book sn=“SN-0002"> … </book>
Save DOM Object into a XML file <? php $dom = new DOMDocument("1. 0", "utf-8"); $root = $dom->create. Element('book'); $root = $dom->append. Child($root); $auth = $dom->create. Element('auth'); $auth = $root->append. Child($auth); $text = $dom->create. Text. Node('This is the title'); $text = $auth->append. Child($text); echo $dom->save("test. xml"); ? > <? xml version="1. 0" encoding="utf-8"? > <book><auth>This is the title</auth></book> test. xml
- Slides: 17