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,

Slides:



Advertisements
Similar presentations
HTML DOM Part-II. Create Node We can create a node in JavaScript dynamically. There are 3 types of Node –Comment –Element –Text Node We can also create.
Advertisements

The Document Object Model
XML CS What is XML?  XML: a "skeleton" for creating markup languages  you already know it!  syntax is identical to XHTML's: content  languages.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science University of Kentucky.
Introduction to XLink Transparency No. 1 XML Information Set W3C Recommendation 24 October 2001 (1stEdition) 4 February 2004 (2ndEdition) Cheng-Chia Chen.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science CSC 560: Management of XML Data.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
CSE 154 LECTURE 23: XML. Storing structured data in arbitrary text formats (bad) My note: BEGIN FROM: Alice Smith TO: Robert Jones.
CSE 154 LECTURE 24: XML AND JSON. Debugging responseXML in Firebug can examine the entire XML document, its node/tree structure.
XML C#.NET Software Development. eXtensible Markup Language Markup language that describes data Markup language that describes data Stores data as text.
1 Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COT 6930 Advanced Internet Programming Dr. Roy Levow Day 2.
1 XML Data Management 4. Domain Object Model Werner Nutt.
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.
JS: Document Object Model (DOM)
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
XML and its applications: 4. Processing XML using PHP.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
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.
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.
© Disruptive Innovations Etna a wysiwyg XML RELAXNG- and Gecko-based editor.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 4 - XML.
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.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
 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.
Web-based Programming Lanjut Pertemuan 9 Matakuliah: M0492 / Web-based Programming Lanjut Tahun: 2007.
WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms.
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
Lecture 3 : PHP5-DOM API UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
JavaScript III ECT 270 Robin Burke. Outline Form validation Regular expressions DOM JS document model review W3C DOM Cross-browser scripting Style.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
1 DHTML and DOM. 2 Style Sheets: CSS and CSS2 W3C recommendation on Cascading Style Sheet CSS level 1 specification allows expressing style of contents.
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
XML DOM.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Working with Elements and Attributes Using DOM Eugenia Fernandez IUPUI.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
XML CORE CSC1310 Fall XML DOCUMENT XML document XML document is a convenient way for parsers to archive data. In other words, it is a way to describe.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
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 CS What is XML?  XML: a "skeleton" for creating markup languages  you already know it!  syntax is identical to XHTML's: content  languages.
THE DOM.
Unit 4 Representing Web Data: XML
CSE 154 Lecture 12: XML.
CHAPTER 5 DOCUMENT OBJECT MODEL
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
Chapter 7 Representing Web Data: XML
DOM Document Object Model.
In this session, you will learn to:
XML Parsing In PHP.
XML DOM and CSS Instructors: Geoffrey Fox and Bryan Carpenter
XML and its applications: 4. Processing XML using PHP
Presentation transcript:

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, the main Apache server on cosmos.ucc.ie supports only PHP 4 A second Apache server, which supports PHP 5 has just been installed on cosmos –it listens to port 8949 instead of port 80 –thus, URLs must be of the form

DOM support in PHP 5 The DOM library in PHP 5 is documented in Chapter XXX of the manual for PHP 5 –The relevant chapter is available online at DOM support in PHP 5 is object-oriented You may find other people using older PHP functions which were not object- oriented –you should avoid using those functions

DOM support in PHP 5 DOM support in PHP 5 follows the W3C DOM Level 2 standard quite closely –although some Level 3 aspects are included This is one reason why DOM support in PHP 5 is fully object-oriented. It is a good idea, when using DOM support in PHP 5, to cross-refer to the W3C DOM standard DOM support in PHP 5 comprises a set of object classes. –These PHP object classes correspond closely (sometimes exactly) to classes in the DOM standard –Consider some W3C DOM class xxx the corresponding PHP class has a name of the form DOMxxx –for example the class NodeList in W3C DOM has a corresponding class in PHP called DOMNodeList

Example W3C and PHP comparison: the NodeList class In the W3C DOM Level 1, Level 2 and Level 3 specifications, the NodeList class has –only one method item –and one attribute: length The object class DOMNodeList in the PHP DOM library corresponds to this exactly. There is – one method: item –and one attribute: length

Another W3C and PHP comparison In the W3C DOM Level 1 specification, there were six methods for the Node class: insertBefore replaceChild removeChild appendChild hasChildNodes cloneNode The W3C DOM Level 2 specification added three extra methods for the Node class: hasAttributes isSupported normalize The PHP DOMNode class has twelve methods: insertBefore replaceChild removeChild appendChild hasChildNodes cloneNode hasAttributes isSupported normalize lookupNameSpaceURI lookupPrefix isSameNode The last three methods in the PHP DOM library correspond to some of the nine extra methods introduced in DOM Level 3

Another W3C and PHP comparison contd. In the W3C DOM Level 1 specification, there were eleven attributes for the Node class: nodeType nodeName nodeValue parentNode childNodes firstChild lastChild previousSibling nextSibling attributes ownerDocument The W3C DOM Level 2 specification added three extra attributes for the Node class: namespaceURI prefix localName The W3C DOM Level 3 specification added two extra attributes for the Node class: baseURI textContent All these attributes are supported in the PHP DOMNode class, which provides sixteen attributes: nodeType nodeName nodeValue parentNode childNodes firstChild lastChild previousSibling nextSibling attributes ownerDocument namespaceURI prefix localName baseURI textContent

Example PHP program Suppose we have an XML file which contains Toyota Honda Ford Suppose its URL is Suppose we want to write a PHP program which will count the number of company elements that are in this file A program to do this is on the next slide

<?php $string = file_get_contents(‘ $doc = new DOMDocument(); $doc->loadXML($string); // parses string into a document object model echo " "; $num= $doc->getElementsByTagName("company")->length; echo ”There are $num company elements in the file"; echo " "; ?>

Output from previous program Note the URL uses port number 8949

The PHP DOMDocument class Extends DOMNode Methods  createAttribute() - Create new attribute createAttribute()  createAttributeNS() - Create new attribute node with an associated namespace createAttributeNS()  createCDATASection() - Create new cdata node createCDATASection()  createComment() - Create new comment node createComment()  createDocumentFragment() - Create new document fragment createDocumentFragment()  createElement() - Create new element node createElement()  createElementNS() - Create new element node with an associated namespace createElementNS()  createEntityReference() - Create new entity reference node createEntityReference()  createProcessingInstruction() - Creates new PI node createProcessingInstruction()  createTextNode() - Create new text node createTextNode()

The PHP DOMDocument class contd. getElementById() - Searches for an element with a certain idgetElementById()  getElementsByTagName() - Searches for all elements with given tag name  getElementsByTagNameNS() - Searches for all elements with given tag name in specified namespace getElementsByTagNameNS()  importNode() - Import node into current document importNode()  load() - Load XML from a file  loadHTML() - Load HTML from a string loadHTML()  loadHTMLFile() - Load HTML from a file loadHTMLFile()  loadXML() - Load XML from a string  normalize() - Normalizes document normalize()  relaxNGValidate() - Performs relaxNG validation on the document relaxNGValidate()  relaxNGValidateSource() - Performs relaxNG validation on the document relaxNGValidateSource()

The PHP DOMDocument class contd. save() - Dumps the internal XML tree back into a filesave()  saveHTML() - Dumps the internal document into a string using HTML formatting saveHTML()  saveHTMLFile() - Dumps the internal document back into a file using HTML formatting saveHTMLFile()  saveXML() - Dumps the internal XML tree back into a string saveXML()  schemaValidate() - Validates a document based on a schema schemaValidate()  schemaValidateSource() - Validates a document based on a schema schemaValidateSource()  validate() - Validates the document based on its DTD validate()  xinclude() - Substitutes XIncludes in a DOMDocument Object xinclude()

The PHP DOMDocument class contd. Attributes (name followed by type) actualEncoding string config DOMConfiguration doctype DOMDocumentType documentElement DOMElement documentURI string encoding string formatOutput bool implementation DOMImplementation preserveWhiteSpace bool recover bool resolveExternals bool standalone bool strictErrorChecking bool substituteEntities bool

The PHP DOMDocument class contd. Attributes (contd.) validateOnParse bool version string xmlEncoding string xmlStandalone bool xmlVersion string

Example application of the PHP DOM library Data exchange between companies

Using XML for data-exchange Suppose we have 10 companies which interact with each other Suppose they all have different database formats Every company needs to be able to exchange data with every other company If we build programs to effect direct db-to-db conversions, we will need 90 different conversion programs But if we use XML as an intermediate format, we will need “only” 20 different conversion programs Put this another way: –When a company joins an “extended enterprise” which already contains N companies, if XML is used for data-exchange, the new company must write 2 conversion programs and the existing companies need do nothing if direct db-to-db conversion is used, the new company must write N conversion programs and each of the existing N companies must write one conversion program the difference is 2 programs versus 2N programs when a new company joins a group of N existing companies

Example of XML-based data exchange Suppose that two companies, ABC and XYZ, have decided to amalgamate They want to share data on the partner companies from whom they get their supplies ABC’s existing database, called corporate, has a table called partners with the following fields: companyVATNumber and companyName XYZ’s existing database, called mainDB, has a table called suppliers with the following fields: vatNumber and name ABC and XYZ decide to exchange data via XML

Example of XML-based data exchange (contd.) ABC and XYZ agree on the following DTD for XML documents which describe their suppliers Each company must write two programs: –one program to copy their database table into an XML document having the format specified above; –another program to read XML documents in the above format and insert the information into their database If they ABC-XYZ consortium later takes over any more companies, each of the new companies will have to write their own versions of the above two programs

ABC’s XML-generation program <?php header('Content-Type: application/xml'); echo ' '; $db=mysql_connect("localhost",”username",”password"); mysql_select_db("corporate",$db); $result=mysql_query("select * from partners",$db); while ($row=mysql_fetch_array($result)) {$companyID=$row["companyID"]; $companyName=$row["companyName"]; ?> <?php } echo ' '; ?>

ABC’s XML-input program <?php$url = $_POST['url']; if (!$url) {?> Supplier data acquisition "> URL of XML file: Submit <?php } else { Code is on next slide } ?>

ABC’s XML-input program (continued) <?php $string=file_get_contents($url); $doc= new DOMDocument(); $doc->loadXML($string); $supplierElements=$doc->getElementsByTagName("supplier"); $numSuppliers = $supplierElements->length; $db=mysql_connect("localhost","username","password"); mysql_select_db("corporate",$db); for ($i=0; $i <= $numSuppliers-1; $i=$i+1) {$supplierElement = $supplierElements->item($i); $vatNumberElement= $supplierElement->getElementsByTagName("vatNumber")->item(0); $vatNumber = $vatNumberElement->nodeValue; $nameElement= $supplierElement->getElementsByTagName("name")->item(0); $name = $nameElement->nodeValue; mysql_query("insert into partners values ($vatNumber,'$name')”,$db); } ?>

XYZ’s XML-generation program <?php header('Content-Type: application/xml'); echo ' '; $db=mysql_connect("localhost",”username2",”password2"); mysql_select_db("mainDB",$db); $result=mysql_query("select * from suppliers",$db); while ($row=mysql_fetch_array($result)) {$vatNumber=$row["vatNumber"]; $name=$row["name"]; ?> <?php } echo ' '; ?>

XYZ’s XML-input program <?php$url = $_POST['url']; if (!$url) {?> Supplier data acquisition "> URL of XML file: Submit <?php } else { Code is on next slide } ?>

XYZ’s XML-input program (continued) <?php $string=file_get_contents($url); $doc= new DOMDocument(); $doc->loadXML($string); $supplierElements=$doc->getElementsByTagName("supplier"); $numSuppliers = $supplierElements->length; $db=mysql_connect("localhost","username2","password2"); mysql_select_db("mainDB",$db); for ($i=0; $i <= $numSuppliers-1; $i=$i+1) {$supplierElement = $supplierElements->item($i); $vatNumberElement= $supplierElement->getElementsByTagName("vatNumber")->item(0); $vatNumber = $vatNumberElement->nodeValue; $nameElement= $supplierElement->getElementsByTagName("name")->item(0); $name = $nameElement->nodeValue; mysql_query("insert into partners values ($vatNumber,'$name')”,$db); } ?>