1.XML_Beautifier
用于将一段排版凌乱的XML文档美化
1 <?php 2 require_once "XML/Beautifier.php"; 3 $fmt = new XML_Beautifier(); 4 $result = $fmt->formatFile(‘originalFile.xml‘, ‘beautifiedFile.xml‘); 5 if (PEAR::isError($result)) { 6 echo $result->getMessage(); 7 exit(); 8 } 9 echo "File beautified, awaiting orders!<br />"; 10 ?>
2.XML_DTD
引用外部的dtd文件以对xml内容进行验证
1 <?php 2 require_once ‘XML/DTD/XmlValidator.php‘; 3 $dtd_file = realpath(‘myfile.dtd‘); 4 $xml_file = realpath(‘myfile.xml‘); 5 $validator = new XML_DTD_XmlValidator(); 6 if (!$validator->isValid($dtd_file, $xml_file)) { 7 die($validator->getMessage()); 8 } 9 ?>
3.XML_Feed_Parser
解析各种主流格式的Feed(xml,atom,rss1,rss2等格式)
1 <?php 2 /* The file you wish to parse */ 3 $source = ‘my_source.xml‘; 4 /* Where Relax NG is available, we can force validation of the feed */ 5 $validate = true; 6 /* Whether or not to suppress non-fatal warnings */ 7 $suppress_warnings = false; 8 /* If the feed is not valid XML and the tidy extension is installed we can * attempt to use it to fix the feed */ 9 $use_tidy = true; 10 $feed = new XML_Feed_Parser($source, $validate, $suppress_warnings, $use_tidy); 11 foreach ($feed as $entry) { 12 print $entry->title . "\n"; 13 } 14 $first_entry = $feed->getEntryByOffset(0); 15 $particular_entry = $feed->getEntryById(‘http://jystewart.net/entry/1‘); 16 ?>
4.XML_Parser
SAX模型的 XML 解析器,相对于DOM解析器在解析时需要将这个xml树加载到内存中,SAX在某些应用中表现的更加轻量级。
1 <?php 2 require_once ‘XML/Parser.php‘; 3 class myParser extends XML_Parser { 4 function myParser() { 5 parent::XML_Parser(); 6 } 7 /** * 处理起始标签 * * @access private * @param resource xml parser 句柄 * @param string 标签名 * @param array 属性值 */ 8 function startHandler($xp, $name, $attribs) { 9 printf(‘handle start tag: %s<br />‘, $name); 10 } 11 /** * 处理结束标签 * * @access private * @param resource xml parser 句柄 * @param string 标签名 */ 12 function endHandler($xp, $name) { 13 printf(‘handle end tag: %s<br />‘, $name); 14 } 15 /** * 处理字符数据 * * @access private * @param resource xml parser 句柄 * @param string 字符数据 */ 16 function cdataHandler($xp, $cdata) { 17 // does nothing here, but might e.g. print $cdata 18 } 19 } 20 21 $p = &new myParser(); 22 $result = $p->setInputFile(‘xml_parser_file.xml‘); 23 $result = $p->parse(); 24 ?>
5.XML_Query2XML
实用query语句直接从数据库中调取数据并转换成XML(支持PDO, PEAR MDB2, PEAR DB, ADOdb)
1 <?php 2 require_once ‘XML/Query2XML.php‘; 3 $pdo = new PDO(‘mysql://[email protected]/Query2XML_Tests‘); 4 $query2xml = XML_Query2XML::factory($pdo); 5 $artistid = $_REQUEST[‘artistid‘]; 6 $dom = $query2xml->getXML( 7 array( 8 ‘data‘ => array( 9 ":$artistid" 10 ), 11 ‘query‘ => ‘SELECT * FROM artist WHERE artistid = ?‘ 12 ), 13 array( 14 ‘rootTag‘ => ‘favorite_artist‘, 15 ‘idColumn‘ => ‘artistid‘, 16 ‘rowTag‘ => ‘artist‘, 17 ‘elements‘ => array( 18 ‘name‘, 19 ‘birth_year‘, 20 ‘music_genre‘ => ‘genre‘ 21 ) 22 ) 23 ); 24 header(‘Content-Type: application/xml‘); 25 $dom->formatOutput = true; 26 print $dom->saveXML(); 27 ?>
6.XML_RDDL
读取资源目录描述语言(Resource Directory Description Language,RDDL)
1 <?php 2 require_once "XML/RDDL.php"; 3 4 // create new RDDL parser 5 $rddl = &new XML_RDDL(); 6 7 // parse a document that contains RDDL resources 8 $result = $rddl->parseRDDL(‘http://www.rddl.org‘); 9 // check for error 10 if (PEAR::isError($result)) { 11 echo sprintf( "ERROR: %s (code %d)", $result->getMessage(), $result->getCode()); 12 exit; 13 } 14 15 // get all resources 16 $resources = $rddl->getAllResources(); 17 echo "<pre>"; 18 print_r($resources); 19 echo "</pre>"; 20 21 // get one resource by its Id 22 $test = $rddl->getResourceById(‘CSS‘); 23 echo "<pre>"; 24 print_r($test); 25 echo "</pre>"; 26 27 // get all stylesheets 28 $test = $rddl->getResourcesByNature(‘http://www.w3.org/1999/XSL/Transform‘); 29 echo "<pre>"; 30 print_r($test); 31 echo "</pre>"; 32 33 // get all normative references 34 $test = $rddl->getResourcesByPurpose(‘http://www.rddl.org/purposes#normative-reference‘); 35 echo "<pre>"; 36 print_r($test); 37 echo "</pre>"; 38 ?>
7.XML_RSS
RSS解析器
1 <?php 2 require_once "XML/RSS.php"; 3 4 $rss =& new XML_RSS("http://rss.slashdot.org/Slashdot/slashdot"); 5 $rss->parse(); 6 7 echo "<h1>Headlines from <a href=\"http://slashdot.org\">Slashdot</a></h1>\n"; 8 echo "<ul>\n"; 9 10 foreach ($rss->getItems() as $item) { 11 echo "<li><a href=\"" . $item[‘link‘] . "\">" . $item[‘title‘] . "</a></li>\n"; 12 } 13 14 echo "</ul>\n"; 15 ?>
8.XML_Serializer
XML生成器
1 <?php 2 require_once ‘XML/Serializer.php‘; 3 4 $options = array( 5 "indent" => " ", 6 "linebreak" => "\n", 7 "typeHints" => false, 8 "addDecl" => true, 9 "encoding" => "UTF-8", 10 "rootName" => "rdf:RDF", 11 "defaultTagName" => "item" 12 ); 13 14 $stories[] = array( 15 ‘title‘ => ‘First Article‘, 16 ‘link‘ => ‘http://freedomink.org/node/view/55‘, 17 ‘description‘ => ‘Short blurb about article........‘ 18 ); 19 20 $stories[] = array( 21 ‘title‘ => ‘Second Article‘, 22 ‘link‘ => ‘http://freedomink.org/node/view/11‘, 23 ‘description‘ => ‘This article shows you how ......‘ 24 ); 25 26 $data[‘channel‘] = array( 27 "title" => "Freedom Ink", 28 "link" => "http://freedomink.org/", 29 $stories 30 ); 31 32 $serializer = new XML_Serializer($options); 33 34 if ($serializer->serialize($data)) { 35 header(‘Content-type: text/xml‘); 36 echo $serializer->getSerializedData(); 37 } 38 ?>
9.XML_sql2xml
通过sql语句直接从数据库中调取数据转化成xml
1 <?php 2 require_once "XML/sql2xml.php"; 3 $sql2xmlclass = new xml_sql2xml("mysql://username:[email protected]/xmltest"); 4 $xmlstring = $sql2xmlclass->getxml("select * from bands"); 5 ?>
10.XML_Statistics
XML数据统计,标签个数,结构深度等,相当好用
1 <?php 2 require_once "XML/Statistics.php"; 3 $stat = new XML_Statistics(array("ignoreWhitespace" => true)); 4 $result = $stat->analyzeFile("example.xml"); 5 6 if ($stat->isError($result)) { 7 die("Error: " . $result->getMessage()); 8 } 9 10 // total amount of tags: 11 echo "Total tags: " . $stat->countTag() . "<br />"; 12 13 // count amount of ‘title‘ attribute, in all tags 14 echo "Occurences of attribute title: " . $stat->countAttribute("title") . "<br />"; 15 16 // count amount of ‘title‘ attribute, only in <section> tags 17 echo "Occurences of attribute title in tag section: " . $stat->countAttribute("title", "section") . "<br />"; 18 19 // count total number of tags in depth 4 20 echo "Amount of Tags in depth 4: " . $stat->countTagsInDepth(4) . "<br />"; 21 22 echo "Occurences of PHP Blocks: " . $stat->countPI("PHP") . "<br />"; 23 24 echo "Occurences of external entity ‘bar‘: " . $stat->countExternalEntity("bar") . "<br />"; 25 26 echo "Data chunks: " . $stat->countDataChunks() . "<br />"; 27 28 echo "Length of all data chunks: " . $stat->getCDataLength() . "<br />";
时间: 2024-10-24 04:01:05