cvs: presentations /slides/xmlandphp dom.helpers.php dom.php parseinto.php sax.callback.php sax.interface.php sax.php

From: Date: Mon, 21 Apr 2003 03:21:45 +0000
Subject: cvs: presentations /slides/xmlandphp dom.helpers.php dom.php parseinto.php sax.callback.php sax.interface.php sax.php
Groups: php.pres 
Request: Send a blank email to pres+get-636@lists.php.net to get a copy of this message
sterling Sun Apr 20 23:21:45 2003 EDT Added files: /presentations/slides/xmlandphp dom.helpers.php dom.php parseinto.php sax.callback.php sax.interface.php sax.php Log: add the php files Index: presentations/slides/xmlandphp/dom.helpers.php +++ presentations/slides/xmlandphp/dom.helpers.php <?php define('URL', 'http://www.amazon.com/exec/obidos/%s/'); function process_item($item) { $id = $item->get_attribute('id'); echo '<a href="'; printf(URL, $id); echo '">'; $children = $item->children(); foreach ($children as $element) { if ($element instanceof domelement) { process_subelement($element); } } } function process_subelement($element) { $name = $element->tagname; $e = array_shift($element->children()); $data = trim($e->content); switch ($name) { case 'title': echo $data; echo "</a>\n"; break; case 'author': echo "<p>By: $data</p>\n"; break; case 'description': echo "<p>\n$data\n</p>\n"; break; } } ?> Index: presentations/slides/xmlandphp/dom.php +++ presentations/slides/xmlandphp/dom.php <?php include_once('dom.helpers.php'); $doc = xmldocfile('thedata.xml'); $wishlist = $doc->root(); $nodes = $wishlist->children(); foreach ($nodes as $node) { if ($node instanceof domelement) { process_item($node); } } ?> Index: presentations/slides/xmlandphp/parseinto.php +++ presentations/slides/xmlandphp/parseinto.php <?php $data = file_get_contents('thedata.xml'); $x = xml_parser_create(); xml_parser_set_option( $x, XML_OPTION_CASE_FOLDING, true ); xml_parser_set_option( $x, XML_OPTION_SKIP_WHITE, true ); xml_parse_into_struct($x, $data, $wishlist, $tags); xml_parser_free($x); // Access All Description Elements $descs = $tags['DESCRIPTION']; foreach ($descs as $descid) { echo $wishlist[$descid]['value']; echo "\n<br />\n"; } ?> Index: presentations/slides/xmlandphp/sax.callback.php +++ presentations/slides/xmlandphp/sax.callback.php <?php function w_start_tag($x, $tag, $attr) { global $w; switch ($tag) { case 'WISHLIST': $w->base = $attr['URI']; break; case 'ITEM': $item = new Wishlist_Item(); $item->id = $attr['ID']; array_push($w->items, $item); $GLOBALS['item'] = $item; break; default: $GLOBALS['curtag'] = $tag; break; } } function w_end_tag($x, $tag) { $GLOBALS['curtag'] = null; } function w_data($x, $data) { global $curtag; global $item; $data = trim($data); switch ($curtag) { case 'TITLE': $item->title .= $data; break; case 'AUTHOR': $item->author .= $data; break; case 'DESCRIPTION': $item->desc .= $data; break; } } ?> Index: presentations/slides/xmlandphp/sax.interface.php +++ presentations/slides/xmlandphp/sax.interface.php <?php class Wishlist { public $base; public $items = array(); } class Wishlist_Item { public $id; public $author; public $title; public $desc; } ?> Index: presentations/slides/xmlandphp/sax.php +++ presentations/slides/xmlandphp/sax.php <?php include_once('sax.interface.php'); include_once('sax.callback.php'); $x = xml_parser_create(); xml_set_element_handler( $x, 'w_start_tag', 'w_end_tag' ); xml_set_character_data_handler( $x, 'w_data' ); xml_parser_set_option( $x, XML_OPTION_CASE_FOLDING, true ); $w = new Wishlist(); $fp = fopen("thedata.xml", "r"); while (!feof($fp)) { $line = fgets($fp, 4096); xml_parse($x, $line); } fclose($fp); var_dump($w->items); ?>

« previous php.pres (#636) next »