PHP XML DOM PHP XML Analiza structurii XML

  • Slides: 31
Download presentation
 PHP XML DOM

PHP XML DOM

PHP XML • Analiza structurii XML se numeşte parsing iar instrumentul pentru analiza dată

PHP XML • Analiza structurii XML se numeşte parsing iar instrumentul pentru analiza dată se numeşte parser. Există două tipuri de XML parseri: • Parser bazat pe arbore (Tree-based parser): Acest parser transformă XML document într-o structură arborescentă. E analizează documentul în întregime şi realizează acces la orice element al documentului ca nodul arborelui (Document Object Model (DOM)) • Parser bazat pe evenimente (Event-based parser): Acest parser percepe documentul XML ca o secvenţă de evenimente. Pentru fiecare eveniment apărut se apelează funcţia pentru tratarea lui.

PHP XML - Expat parser Parserul Expat este partea-componentă a pachetului de bază a

PHP XML - Expat parser Parserul Expat este partea-componentă a pachetului de bază a PHP şi nu necesită instalare suplimentară. Parserul Expat este bazat pe evenimente (event-based parser). Parserul bazat pe evenimente se concentrează pe conţinutul documentului, nu pe structura lui. Din cauza aceasta el este mai rapid decît parserul bazat pe arbore. Exemplu: în fragmentul dat <from>Luchianov Ludmila</from> parserul bazat pe evenimente observă o serie de evenimente: Elementul de start: from Secţia CDATA cu valoarea: Luchianov Ludmila Elementul de închidere: from

PHP XML DOM Parserul DOM este partea-componentă a pachetului de bază a PHP şi

PHP XML DOM Parserul DOM este partea-componentă a pachetului de bază a PHP şi nu necesită instalare suplimentară. Parserul DOM este bazat pe arbore (treebased parser). Parserul bazat pe arbore percepe documentul structurat ca un graf arborescent în care elementele documentului sunt nodurile grafului.

Crearea sructurii (DOMDocument-Object ), încărcarea documentului şi vizualizarea lui <? php $xml. Doc =

Crearea sructurii (DOMDocument-Object ), încărcarea documentului şi vizualizarea lui <? php $xml. Doc = new DOMDocument(); $xml. Doc->load(“books. xml"); print $xml. Doc->save. XML(); ? > P. S. save. XML formează un rînd de text cu documentul XML dat

Rezultat în browser Giada De Laurentiis 2005 30. 00 J K. Rowling 2005 29.

Rezultat în browser Giada De Laurentiis 2005 30. 00 J K. Rowling 2005 29. 99 James Mc. Govern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49. 99 Erik T. Ray 2003 39. 95 Source: <? xml version="1. 0" encoding="ISO-8859 -1"? > <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30. 00</price> </book> . . .

Navigarea prin noduri <? php $xml. Doc = new DOMDocument(); $xml. Doc->load("books. xml"); $x

Navigarea prin noduri <? php $xml. Doc = new DOMDocument(); $xml. Doc->load("books. xml"); $x = $xml. Doc->document. Element; foreach ($x->child. Nodes AS $item) { print $item->node. Name. " = ". $item->node. Value. "<br />"; }

Rezultat • #text = book = Everyday Italian Giada De Laurentiis 2005 30. 00

Rezultat • #text = book = Everyday Italian Giada De Laurentiis 2005 30. 00 #text = book = Harry Potter J K. Rowling 2005 29. 99 #text = book = XQuery Kick Start James Mc. Govern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49. 99 #text = book = Learning XML Erik T. Ray 2003 39. 95 #text =

Navigarea prin noduri <? php $xml. Doc = new DOMDocument(); $xml. Doc->load("books. xml"); $x

Navigarea prin noduri <? php $xml. Doc = new DOMDocument(); $xml. Doc->load("books. xml"); $x = $xml. Doc->document. Element; foreach ($x->child. Nodes AS $item) { if ($item->node. Name != "#text") print $item->node. Name. " = ". $item->node. Value. "<br />"; }

Rezultat • book = Everyday Italian Giada De Laurentiis 2005 30. 00 book =

Rezultat • book = Everyday Italian Giada De Laurentiis 2005 30. 00 book = Harry Potter J K. Rowling 2005 29. 99 book = XQuery Kick Start James Mc. Govern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49. 99 book = Learning XML Erik T. Ray 2003 39. 95

Exemple <? php $xml. Doc = new DOMDocument(); $xml. Doc->load("books. xml"); $book = $xml.

Exemple <? php $xml. Doc = new DOMDocument(); $xml. Doc->load("books. xml"); $book = $xml. Doc->get. Elements. By. Tag. Name('book'); foreach ($book as $elem) { echo "<p>"; echo "Title: "; echo $g=$elem->get. Elements. By. Tag. Name('title')->item(0)->node. Value; echo "<br/>Author: "; echo $g=$elem->get. Elements. By. Tag. Name('author')->item(0)->node. Value; echo "<br/>Year: "; echo $g=$elem->get. Elements. By. Tag. Name('year')->item(0)->node. Value; echo "<br/>Price: "; echo $g=$elem->get. Elements. By. Tag. Name('price')->item(0)->node. Value; echo "</p>"; } ? >

Exemple $nb = $book->length; echo $nb; echo "<p>"; for ($i = 0; $i<$nb; $i++

Exemple $nb = $book->length; echo $nb; echo "<p>"; for ($i = 0; $i<$nb; $i++ ) { $el = $book->item($i); echo "Title: "; echo $g=$el->get. Elements. By. Tag. Name('title')->item(0)->node. Value; echo "<br/>Author: "; echo $g=$el->get. Elements. By. Tag. Name('author')->item(0)->node. Value; echo "<br/>Year: "; echo $g=$el->get. Elements. By. Tag. Name('year')->item(0)->node. Value; echo "<br/>Price: "; echo $g=$el->get. Elements. By. Tag. Name('price')->item(0)->node. Value; echo "</p>"; }

Rezultat Title: Everyday Italian Author: Giada De Laurentiis Year: 2005 Price: 30. 00 Title:

Rezultat Title: Everyday Italian Author: Giada De Laurentiis Year: 2005 Price: 30. 00 Title: Harry Potter Author: J K. Rowling Year: 2005 Price: 29. 99 Title: XQuery Kick Start Author: James Mc. Govern Year: 2003 Price: 49. 99 Title: Learning XML Author: Erik T. Ray Year: 2003 Price: 39. 95

Adăugarea elementelor $ed=array("Springer", "Pandeia", "Litera", "Nova"); echo "<p>"; for ($i = 0; $i<$nb; $i++

Adăugarea elementelor $ed=array("Springer", "Pandeia", "Litera", "Nova"); echo "<p>"; for ($i = 0; $i<$nb; $i++ ) { $el = $book->item($i); $edited = $xml. Doc->create. Element('edited', $ed[$i]); $el->append. Child($edited); echo "Title: "; echo $g=$el->get. Elements. By. Tag. Name('title')->item(0)->node. Value; echo "<br/>Edited: "; echo $g=$el->get. Elements. By. Tag. Name('edited')->item(0)->node. Value; echo "<br/>Author: "; . . . echo "</p>"; }

Rezultat Title: Everyday Italian Edited: Springer Author: Giada De Laurentiis Year: 2005 Price: 30.

Rezultat Title: Everyday Italian Edited: Springer Author: Giada De Laurentiis Year: 2005 Price: 30. 00 Title: Harry Potter Edited: Pandeia Author: J K. Rowling Year: 2005 Price: 29. 99 Title: XQuery Kick Start Edited: Litera Author: James Mc. Govern Year: 2003 Price: 49. 99 Title: Learning XML Edited: Nova Author: Erik T. Ray Year: 2003 Price: 39. 95

PHP Simple. XML este o extensie versiunii 5 PHP şi permite crearea, navigarea şi

PHP Simple. XML este o extensie versiunii 5 PHP şi permite crearea, navigarea şi scimbarea documentelor XML în mod simplu.

Navigarea prin XML cu Simple. XML <? php $xml = simplexml_load_file("books. xml"); echo $xml->get.

Navigarea prin XML cu Simple. XML <? php $xml = simplexml_load_file("books. xml"); echo $xml->get. Name(). "<br />"; foreach($xml->children() as $child) { echo $child->get. Name(). ": ". $child. "<br />"; foreach($child->children() as $ch) { echo $ch->get. Name(). ": ". $ch. "<br />"; } } ? >

Rezultat Bookstore book: title: Everyday Italian author: Giada De Laurentiis year: 2005 price: 30.

Rezultat Bookstore book: title: Everyday Italian author: Giada De Laurentiis year: 2005 price: 30. 00 book: title: Harry Potter author: J K. Rowling year: 2005 price: 29. 99 book: title: XQuery Kick Start author: James Mc. Govern author: Per Bothner author: Kurt Cagle author: James Linn author: Vaidyanathan Nagarajan year: 2003 price: 49. 99 . . .

Obţinerea atributelor <? php $xml = simplexml_load_file("books. xml"); echo $xml->get. Name(). "<br />"; foreach($xml->children()

Obţinerea atributelor <? php $xml = simplexml_load_file("books. xml"); echo $xml->get. Name(). "<br />"; foreach($xml->children() as $child) { $childname=$child->get. Name(); echo $childname. ": ". $child. "<br />"; foreach($child->attributes() as $a => $b) { echo $a, '="', $b, ""</br>"; } foreach($child->children() as $ch) { $cname=$ch->get. Name(); echo $cname. ": ". $ch. "<br />"; } } ? >

Rezultat bookstore book: category="cooking" title: Everyday Italian author: Giada De Laurentiis year: 2005 price:

Rezultat bookstore book: category="cooking" title: Everyday Italian author: Giada De Laurentiis year: 2005 price: 30. 00 . . . book: category="web" title: XQuery Kick Start author: James Mc. Govern author: Per Bothner author: Kurt Cagle author: James Linn author: Vaidyanathan Nagarajan year: 2003 price: 49. 99 book: category="web" cover="paperback" title: Learning XML author: Erik T. Ray year: 2003 price: 39. 95

XPath (XML Path Language) este un limbaj de expresii utilizat pentru a selecta porțiuni

XPath (XML Path Language) este un limbaj de expresii utilizat pentru a selecta porțiuni dintr-un document XML sau pentru a calcula valori (șiruri de caractere, numere, sau valori buleene) pe baza conținutului unui document XML. Versiunea actuală a limbajului este XPath 2. 0, dar cea mai întâlnită versiune în prezent este versiunea 1. 0.

Sintaxa XPath Nodename Selectează toți copii nodului dat / Selectează de la nod rădăcină

Sintaxa XPath Nodename Selectează toți copii nodului dat / Selectează de la nod rădăcină // Selectează nodurile începînd de la nodul curent care corespund condiției . Selectează nodul curent. . Selectează parintele nodului curent @ Selectează atributele

Sintaxa XPath (exemple) bookstore Selectează toți copiii elementului dat /bookstore Selectează elementul bookstore/book Selectează

Sintaxa XPath (exemple) bookstore Selectează toți copiii elementului dat /bookstore Selectează elementul bookstore/book Selectează elemente book copii nodului bookstore //book Selectează toate elementele book din document bookstore//book Selectează toate elementele book care sunt sub elementul bookstore //@lang Selectează toate atributele numite lang

Exemplu cu XPATH <? php // isbn => pages $page_numbers = array( '978 -1594489501'

Exemplu cu XPATH <? php // isbn => pages $page_numbers = array( '978 -1594489501' => '384', // A Thousand Splendid Suns '978 -1594489587' => '352', // The Brief Wondrous Life of Oscar Wao '978 -0545010221' => '784', // Harry Potter and the Deathly Hallows ); $dom = new DOMDocument(); $dom->load('books. xml'); $xpath = new DOMXPath($dom); $books = $xpath->query('book'); foreach($books as $book) { $book->append. Child($dom->create. Element('pages', $page_numbers[$book>get. Attribute('isbn')])); } $dom->save('books 2. xml'); ? >

Rezultat <book isbn="978 -1594489501" category="web"> <title lang="en">XQuery Kick Start</title> <author>James Mc. Govern</author> <author>Per Bothner</author>

Rezultat <book isbn="978 -1594489501" category="web"> <title lang="en">XQuery Kick Start</title> <author>James Mc. Govern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49. 99</price> </book> <book isbn="978 -1594489501" category="web"> <title lang="en">XQuery Kick Start</title> <author>James Mc. Govern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49. 99</price> <pages>384</pages> </book>

<? php function parse_recursive(Simple. XMLElement $element, $level = 0) { $indent = str_repeat("  ",

<? php function parse_recursive(Simple. XMLElement $element, $level = 0) { $indent = str_repeat("  ", $level); // determine how much we'll indent $value = trim((string) $element); // get the value and trim any whitespace from the start and end $attributes = $element->attributes(); // get all attributes $children = $element->children(); // get all children echo "{$indent}Parsing '{$element->get. Name()}'. . . ". "<br/>"; if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children { echo "{$indent}Value: {$element}". "<br/>"; } if(count($attributes) > 0) {// only show attributes if there any echo $indent. 'Has '. count($attributes). ' attribute(s): '. "<br/>"; foreach($attributes as $attribute) { echo "{$indent}- {$attribute->get. Name()}: {$attribute}". "<br/>"; } } if(count($children)) {// only show children if there any Recursive parsing echo $indent. 'Has '. count($children). ' child(ren): '. "<br/>"; Pentru orice XML foreach($children as $child) { parse_recursive($child, $level+1); // recursion : ) document } } echo $indent. "<br/>"; // just to make it "cleaner" } $xml = new Simple. XMLElement('books. xml', null, true); parse_recursive($xml); ? >

Parsing 'bookstore'. . . Has 4 child(ren): Parsing 'book'. . . Has 1 attribute(s):

Parsing 'bookstore'. . . Has 4 child(ren): Parsing 'book'. . . Has 1 attribute(s): - category: cooking Has 4 child(ren): Parsing 'title'. . . Value: Everyday Italian Has 1 attribute(s): - lang: en Parsing 'author'. . . Value: Giada De Laurentiis Parsing 'year'. . . Value: 2005 Parsing 'price'. . . Value: 30. 00 Parsing 'book'. . . Has 1 attribute(s): - category: children Has 4 child(ren): Parsing 'title'. . . Value: Harry Potter Has 1 attribute(s): - lang: en Parsing 'author'. . . Value: J K. Rowling Rezultat

Concurenţii • Active Server Pages (ASP), corp. Microsoft • Cold. Fusion corp. Allaire.

Concurenţii • Active Server Pages (ASP), corp. Microsoft • Cold. Fusion corp. Allaire.

Avantajele PHP • Viteza. Programele PHPlucrează ma repede decît ASP. • Comoditatea. Programele PHP

Avantajele PHP • Viteza. Programele PHPlucrează ma repede decît ASP. • Comoditatea. Programele PHP pot fi create cît în cadrul documentului HTML aşa şi independent de acesta ce simplifică colaborarea între web-developeri şi webdesigneri. • Preţul. PHP este free software. • Simpltatea utilizării. Progaramatorii cu experienţă în alte limbaje de programare găsesc sintaxa PHP foarte similară cu limbajele date. • PHP este universal. Aceaşi cod PHPpoate fi rulat şi în mediu NTşi pe sisteme din familia UNIX.