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

Slides:



Advertisements
Similar presentations
Advanced XSLT II. Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined.
Advertisements

Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
Advanced XSLT II. Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined.
Introduction to the Logical Structure of XML Documents Web Engineering, SS 2007 Tomáš Pitner, Michael Derntl.
CG0119 Web Database Systems Parsing XML using SimpleXML.
CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 12 Extensible Stylesheet Language Transformations : XSLT.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
XML & Data Structures for the Internet Yingcai Xiao.
UFCEKG-20-2 Data, Schemas & Applications Lecture 5 XML & PHP.
1 CP3024 Lecture 9 XML revisited, XSL, XSLT, XPath, XSL Formatting Objects.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
Lecture 14 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
XSL Unit 6 November 2. XSL –eXtensible Stylesheet Language –Basically a stylesheet for XML documents XSL has three parts: –XSLT –XPath –XSL-FO.
Extensible Markup Language XML MIS 520 – Database Theory Fall 2001 (Day) Lecture 14.
HTML Elements. HTML documents are defined by HTML elements.
PHP with XML Dequan Chen and Narith Kun ---Term Project--- for WSU 2010 Summer Course - CS366 s:
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
Lecture 15 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
XML eXtensible Markup Language by Darrell Payne. Experience Logicon / Sterling Federal C, C++, JavaScript/Jscript, Shell Script, Perl XML Training XML.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML)‏
XML and its applications: 4. Processing XML using PHP.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
WORKING WITH XSLT AND XPATH
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Lecture 2 : Understanding the Document Object Model (DOM) UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
1 © Netskills Quality Internet Training, University of Newcastle Introducing XML © Netskills, Quality Internet Training University.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
What is XML?  XML stands for EXtensible Markup Language  XML is a markup language much like HTML  XML was designed to carry data, not to display data.
The PHP5-DOM API. Outline Create DOM Object Load XML from a file/string Manipulate DOM Object – Search/Delete/Create/Replace DOMNode – Set/Get DOMAttribute.
 XML is designed to describe data and to focus on what data is. HTML is designed to display data and to focus on how data looks.  XML is created to structure,
Session IV Chapter 9 – XML Schemas
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
Lecture 14 Extensible Stylesheet Language Transformations : XSLT.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
Intro to XML Originally Presented by Clifford Lemoine Modified by Box.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter X IXXXXXXXXXX XXXXXX.
XPath Aug ’10 – Dec ‘10. XPath   XML Path Language   Technology that allows to select a part or parts of an XML document to process   XPath was.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
School of Computing and Information Systems CS 371 Web Application Programming XML and JSON Encoding Data.
Web Technologies COMP6115 Session 4: Adding a Database to a Web Site Dr. Paul Walcott Department of Computer Science, Mathematics and Physics University.
An Introduction to XML Paul Donohue May 8th 2002 Hotel Senator Zürich.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
Unit 3 — Advanced Internet Technologies Lesson 11 — Introduction to XSL.
What is XML? eXtensible Markup Language eXtensible Markup Language A subset of SGML (Standard Generalized Markup Language) A subset of SGML (Standard Generalized.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Structured-Document Processing Languages Spring 2004 Course Review Repetitio mater studiorum est!
XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name value pair;
XSLT: How Do We Use It? Nancy Hallberg Nikki Massaro Kauffman.
 Packages:  Scrapy, Beautiful Soup  Scrapy  Website  
Module Road Map Assignment Road Map Notice we have linked the conduit directly to the presentation layer. This is normally a bad idea!
What is it? –w3c standard for… Navigating through an XML document Filtering & finding information –Includes… Expressions for navigating to parts of an.
Lecture 15 Extensible Stylesheet Language Transformations : XSLT.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
XML Extensible Markup Language
Unit 4 Representing Web Data: XML
Java XML IS
XML in Web Technologies
Chapter 7 Representing Web Data: XML
More Sample XML By Sadia Anjum.
XML and its applications: 4. Processing XML using PHP
Presentation transcript:

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

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

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 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

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.

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(); ?>

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(); ?>

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

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 : matches any a with an attribute named “att” matches any tag called a with an attribute named “att” with the value “val”

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

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

Example for DOMXPath::query <?php $dom = new DOMDocument("1.0", "utf-8"); $dom->load("book.xml"); $xpath = new DOMXPath($dom); $nodes = 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

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

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!

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

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

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(); ?> … … … …

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