Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 : PHP5-DOM API UFCFR5-15-3 Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.

Similar presentations


Presentation on theme: "Lecture 3 : PHP5-DOM API UFCFR5-15-3 Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong."— Presentation transcript:

1 Lecture 3 : PHP5-DOM API UFCFR5-15-3 Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong

2 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

3 a simple element containing text 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 (“) XML document structure (recap) Joseph Michael Bloggs Joe Mr 2 Gloucester Road Bristol Avon BS2 4QS 0117 9541054 07710 234674 joe.bloggs@email.com xml declaration (optional) used by xml processor; this documents conforms to xml version 1 and uses the UTF-8 standard (Unicode optimized for ASCII) root element; every well formed xml document must be enclosed by exactly one root element. empty elements a complex element containing other elements and text a comment; comments must be delimited by the characters as in xhtml

4 PHP5 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.

5 Load XML from a file o Syntax DOMDocument::load ( string $filename [, int $options = 0 ] ) Example <?php $dom = new DOMDocument("1.0", "utf-8"); // load XML from a book.xml $dom->load(" book.xml "); echo $dom->saveXML(); ?>

6 Load XML from a string o Syntax DOMDocument::loadXML ( string $source [, int $options = 0 ] ) o Example <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->loadXML(" "); echo $dom->saveXML(); ?>

7 Search DOMNode o Syntax DOMNodeListDOMNodeList DOMElement::getElementsByTagName ( string $name ) DOMNodeListDOMNodeList DOMXPath::query ( string $expression [, DOMNode $contextnode ] )DOMNode

8 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. 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 31st 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”

9 example file (book.xml) Dai Practical Programming in Tcl and Tk £30.00 Laby Programming PHP £20.00 Hill Thinking in Java £15.00

10 Example for DOMElement:: getElementsByTagName <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->load("book.xml"); $nodes = $dom->getElementsByTagName("book"); foreach($nodes as $n) { echo $n->getAttribute("sn"). " "; } ?> o Output 0001 0002 0003

11 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) { $authNodes = $n->getElementsByTagName("auth"); foreach ($authNodes as $n2) { echo $n2->nodeName." = ".$n2->nodeValue; } ?> o Output Auth = Laby Laby Programming PHP £20.00

12 Example: Delete DOMNode <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->load("book.xml"); $nodes = $dom->getElementsByTagName("book"); foreach ($nodes as $n) { if($n->getAttribute("sn") == "0003") { $parent = $n->parentNode; $parent->removeChild($n); } echo $dom->saveXML(); ?> Dai Practical Programming in Tcl and Tk £30.00 Laby Programming PHP £20.00 Output

13 Example: Create DOMNode <?php $dom = new DOMDocument('1.0', 'utf-8'); $node = $dom- >createElement('test', 'This is the root element!'); $dom->appendChild($node); echo $dom->saveXML(); ?> Output This is the root element!

14 Example: Create DOMNode <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->load("book.xml"); $nodes = $dom->getElementsByTagName("book"); foreach ($nodes as $n) { if($n->getAttribute("sn") == "0001") { $tag = $dom->createElement(‘tag', 'Good'); $n->appendChild($tag); } echo $dom->saveXML(); ?> Dai Practical Programming in Tcl and Tk £30.00 Good Result

15 Example: Replace DOMNode <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->load("book.xml"); $nodes = $dom->getElementsByTagName("book"); foreach ($nodes as $n) { if($n->getAttribute("sn") == "0001") { $newPrice = $dom->createElement('price', ‘£40.00'); $oldPrice = $n->getElementsByTagName("price")->item(0); $n->replaceChild($newPrice,$oldPrice); } echo $dom->saveXML(); ?> Dai Practical Programming in Tcl and Tk £40.00 Result

16 Example: Get/Set DOMAttribute <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->load("book.xml"); $nodes = $dom->getElementsByTagName("book"); foreach ($nodes as $n) { $sn = $n->getAttribute("sn"); $sn = "SN-".$sn; $n->setAttribute("sn",$sn); } echo $dom->saveXML(); ?> … … … …

17 Save DOM Object into a XML file <?php $dom = new DOMDocument("1.0", "utf-8"); $root = $dom->createElement('book'); $root = $dom->appendChild($root); $auth = $dom->createElement('auth'); $auth = $root->appendChild($auth); $text = $dom->createTextNode('This is the title'); $text = $auth->appendChild($text); echo $dom->save("test.xml"); ?> This is the title test.xml


Download ppt "Lecture 3 : PHP5-DOM API UFCFR5-15-3 Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong."

Similar presentations


Ads by Google