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.

Slides:



Advertisements
Similar presentations
Introduction to the Logical Structure of XML Documents Web Engineering, SS 2007 Tomáš Pitner, Michael Derntl.
Advertisements

CG0119 Web Database Systems Parsing XML using SimpleXML.
CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT.
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
UFCEKG-20-2 Data, Schemas & Applications Lecture 5 XML & PHP.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
PHP and the DOM There was some experimental support for the DOM in PHP 4 But it is only with PHP 5 that support appears to have stabilized At present,
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
Creating & Attaching CSS An CSS Primer Tutorial. A New CSS Document Create a new CSS Document in Dreamweaver using the “New” option under the File Menu.
XML Primer. 2 History: SGML vs. HTML vs. XML SGML (1960) XML(1996) HTML(1990) XHTML(2000)
Introduction to XML Extensible Markup Language
PHP with XML Dequan Chen and Narith Kun ---Term Project--- for WSU 2010 Summer Course - CS366 s:
PHP and XML TP2653 Advance Web Programming. PHP and XML PHP5 – XML-based extensions, library and functionalities (current XAMPP PHP version is )
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
CG0119 Web Database Systems Using XPath to Navigate & Filter XML (via SimpleXML)‏
XSLT for Data Manipulation By: April Fleming. What We Will Cover The What, Why, When, and How of XSLT What tools you will need to get started A sample.
XML and its applications: 4. Processing XML using PHP.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Utilizing Data Sets from the CReSIS Data Archives to Visualize Greenland Echograms Information in Google Earth 2012 Research Experience for Undergraduates.
Loading methods in PHP DOMDocument class DOMDocument extends DOMNode {... mixed load ( string $filename.
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.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
 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.
1 Dr Alexiei Dingli XML Technologies XML Advanced.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
XML Document Object Model Anthony Borquez. The Document Object Model a programming interface for HTML and XML documents. It defines the way a document.
WEB BASED DATA TRANSFORMATION USING XML, JAVA Group members: Darius Balarashti & Matt Smith.
PHP 5 Sucks! PHP 5 Rocks! Adam Trachtenberg eBay Technical Evangelist
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
School of Computing and Information Systems CS 371 Web Application Programming XML and JSON Encoding Data.
Lecture 3 : PHP5-DOM API UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
Syntax of the HTML HyperText Markup Language. HTML Syntax  What is it?  Helps computer know how to display  What goes into it?  U+FEFF BYTE ORDER.
Dom and XSLT Dom – document object model DOM – collection of nodes in a tree.
What is XML? eXtensible Markup Language eXtensible Markup Language A subset of SGML (Standard Generalized Markup Language) A subset of SGML (Standard Generalized.
 defined as Extensible Markup Language (XML) is a set of rules for encoding documents  Defines structure and data.
Introduction to XML XML – Extensible Markup Language.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
What is it? –w3c standard for… Navigating through an XML document Filtering & finding information –Includes… Expressions for navigating to parts of an.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
CSE 154 LECTURE 17: WEB SERVICES. What is a web service? web service: software functionality that can be invoked through the internet using common protocols.
XML DOM Week 11 Web site:
XML & JSON. Background XML and JSON are to standard, textual data formats for representing arbitrary data – XML stands for “eXtensible Markup Language”
CSE 154 LECTURE 18: WEB SERVICES. Exercise: Baby name web service Write a web service that accepts a name and gender and finds and outputs the line from.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Javascript and Dynamic Web Pages: Client Side Processing
Unit 4 Representing Web Data: XML
Java XML IS
Introduction to the Document Object Model
Binary search tree. Removing a node
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
The XML Language.
Chapter 7 Representing Web Data: XML
Week 11 Web site: XML DOM Week 11 Web site:
DOM Document Object Model.
Processing XML.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
More Sample XML By Sadia Anjum.
XML Parsing In PHP.
XML and its applications: 4. Processing XML using PHP
Presentation transcript:

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 Save DOM Object into a XML file.

Create a DOM Object Syntax DOMDocument::__construct ([ string $version [, string $encoding ]] ) Example <?php $dom = new DOMDocument(" 1.0 ", "utf-8 "); // Dumps the internal XML tree back into a string echo $dom->saveXML(); ?> Output

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

book.xml Dai Practical Programming in Tcl and Tk $400 Laby Programming PHP $300 Hill Thinking in Java $200

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"). " "; } ?> Output

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 $400 Laby Programming PHP $300 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 $400 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', '$500'); $oldPrice = $n->getElementsByTagName("price")->item(0); $n->replaceChild($newPrice,$oldPrice); } echo $dom->saveXML(); ?> Dai Practical Programming in Tcl and Tk $500 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