1.XML_Beautifier

用于将一段排版凌乱的XML文档美化

 <?php
require_once "XML/Beautifier.php";
$fmt = new XML_Beautifier();
$result = $fmt->formatFile('originalFile.xml', 'beautifiedFile.xml');
if (PEAR::isError($result)) {
echo $result->getMessage();
exit();
}
echo "File beautified, awaiting orders!<br />";
?>

2.XML_DTD

引用外部的dtd文件以对xml内容进行验证

 <?php
require_once 'XML/DTD/XmlValidator.php';
$dtd_file = realpath('myfile.dtd');
$xml_file = realpath('myfile.xml');
$validator = new XML_DTD_XmlValidator();
if (!$validator->isValid($dtd_file, $xml_file)) {
die($validator->getMessage());
}
?>

3.XML_Feed_Parser

解析各种主流格式的Feed(xml,atom,rss1,rss2等格式)

 <?php
/* The file you wish to parse */
$source = 'my_source.xml';
/* Where Relax NG is available, we can force validation of the feed */
$validate = true;
/* Whether or not to suppress non-fatal warnings */
$suppress_warnings = false;
/* If the feed is not valid XML and the tidy extension is installed we can * attempt to use it to fix the feed */
$use_tidy = true;
$feed = new XML_Feed_Parser($source, $validate, $suppress_warnings, $use_tidy);
foreach ($feed as $entry) {
print $entry->title . "\n";
}
$first_entry = $feed->getEntryByOffset(0);
$particular_entry = $feed->getEntryById('http://jystewart.net/entry/1');
?>

4.XML_Parser

SAX模型的 XML 解析器,相对于DOM解析器在解析时需要将这个xml树加载到内存中,SAX在某些应用中表现的更加轻量级。

 <?php
require_once 'XML/Parser.php';
class myParser extends XML_Parser {
function myParser() {
parent::XML_Parser();
}
/** * 处理起始标签 * * @access private * @param resource xml parser 句柄 * @param string 标签名 * @param array 属性值 */
function startHandler($xp, $name, $attribs) {
printf('handle start tag: %s<br />', $name);
}
/** * 处理结束标签 * * @access private * @param resource xml parser 句柄 * @param string 标签名 */
function endHandler($xp, $name) {
printf('handle end tag: %s<br />', $name);
}
/** * 处理字符数据 * * @access private * @param resource xml parser 句柄 * @param string 字符数据 */
function cdataHandler($xp, $cdata) {
// does nothing here, but might e.g. print $cdata
}
} $p = &new myParser();
$result = $p->setInputFile('xml_parser_file.xml');
$result = $p->parse();
?>

5.XML_Query2XML

实用query语句直接从数据库中调取数据并转换成XML(支持PDO, PEAR MDB2, PEAR DB, ADOdb)

 <?php
require_once 'XML/Query2XML.php';
$pdo = new PDO('mysql://root@localhost/Query2XML_Tests');
$query2xml = XML_Query2XML::factory($pdo);
$artistid = $_REQUEST['artistid'];
$dom = $query2xml->getXML(
array(
'data' => array(
":$artistid"
),
'query' => 'SELECT * FROM artist WHERE artistid = ?'
),
array(
'rootTag' => 'favorite_artist',
'idColumn' => 'artistid',
'rowTag' => 'artist',
'elements' => array(
'name',
'birth_year',
'music_genre' => 'genre'
)
)
);
header('Content-Type: application/xml');
$dom->formatOutput = true;
print $dom->saveXML();
?>

6.XML_RDDL

读取资源目录描述语言(Resource Directory Description Language,RDDL)

 <?php
require_once "XML/RDDL.php"; // create new RDDL parser
$rddl = &new XML_RDDL(); // parse a document that contains RDDL resources
$result = $rddl->parseRDDL('http://www.rddl.org');
// check for error
if (PEAR::isError($result)) {
echo sprintf( "ERROR: %s (code %d)", $result->getMessage(), $result->getCode());
exit;
} // get all resources
$resources = $rddl->getAllResources();
echo "<pre>";
print_r($resources);
echo "</pre>"; // get one resource by its Id
$test = $rddl->getResourceById('CSS');
echo "<pre>";
print_r($test);
echo "</pre>"; // get all stylesheets
$test = $rddl->getResourcesByNature('http://www.w3.org/1999/XSL/Transform');
echo "<pre>";
print_r($test);
echo "</pre>"; // get all normative references
$test = $rddl->getResourcesByPurpose('http://www.rddl.org/purposes#normative-reference');
echo "<pre>";
print_r($test);
echo "</pre>";
?>

7.XML_RSS

RSS解析器

 <?php
require_once "XML/RSS.php"; $rss =& new XML_RSS("http://rss.slashdot.org/Slashdot/slashdot");
$rss->parse(); echo "<h1>Headlines from <a href=\"http://slashdot.org\">Slashdot</a></h1>\n";
echo "<ul>\n"; foreach ($rss->getItems() as $item) {
echo "<li><a href=\"" . $item['link'] . "\">" . $item['title'] . "</a></li>\n";
} echo "</ul>\n";
?>

8.XML_Serializer

XML生成器

 <?php
require_once 'XML/Serializer.php'; $options = array(
"indent" => " ",
"linebreak" => "\n",
"typeHints" => false,
"addDecl" => true,
"encoding" => "UTF-8",
"rootName" => "rdf:RDF",
"defaultTagName" => "item"
); $stories[] = array(
'title' => 'First Article',
'link' => 'http://freedomink.org/node/view/55',
'description' => 'Short blurb about article........'
); $stories[] = array(
'title' => 'Second Article',
'link' => 'http://freedomink.org/node/view/11',
'description' => 'This article shows you how ......'
); $data['channel'] = array(
"title" => "Freedom Ink",
"link" => "http://freedomink.org/",
$stories
); $serializer = new XML_Serializer($options); if ($serializer->serialize($data)) {
header('Content-type: text/xml');
echo $serializer->getSerializedData();
}
?>

9.XML_sql2xml

通过sql语句直接从数据库中调取数据转化成xml

 <?php
require_once "XML/sql2xml.php";
$sql2xmlclass = new xml_sql2xml("mysql://username:password@localhost/xmltest");
$xmlstring = $sql2xmlclass->getxml("select * from bands");
?>

10.XML_Statistics

XML数据统计,标签个数,结构深度等,相当好用

 <?php
require_once "XML/Statistics.php";
$stat = new XML_Statistics(array("ignoreWhitespace" => true));
$result = $stat->analyzeFile("example.xml"); if ($stat->isError($result)) {
die("Error: " . $result->getMessage());
} // total amount of tags:
echo "Total tags: " . $stat->countTag() . "<br />"; // count amount of 'title' attribute, in all tags
echo "Occurences of attribute title: " . $stat->countAttribute("title") . "<br />"; // count amount of 'title' attribute, only in <section> tags
echo "Occurences of attribute title in tag section: " . $stat->countAttribute("title", "section") . "<br />"; // count total number of tags in depth 4
echo "Amount of Tags in depth 4: " . $stat->countTagsInDepth(4) . "<br />"; echo "Occurences of PHP Blocks: " . $stat->countPI("PHP") . "<br />"; echo "Occurences of external entity 'bar': " . $stat->countExternalEntity("bar") . "<br />"; echo "Data chunks: " . $stat->countDataChunks() . "<br />"; echo "Length of all data chunks: " . $stat->getCDataLength() . "<br />";

pear中几个实用的xml代码库的更多相关文章

  1. 在Jenkins中使用Git Plugin访问Https代码库失败的问题

    最近需要在Jenkins上配置一个Job,SCM源是http://git.opendaylight.org/gerrit/p/integration.git 于是使用Jenkins的Git Plugi ...

  2. 分享实用的JavaScript代码库

    1 var keyCodeMap = { 2 8: 'Backspace', 3 9: 'Tab', 4 13: 'Enter', 5 16: 'Shift', 6 17: 'Ctrl', 7 18: ...

  3. 在Team Foundation Server (TFS)的代码库或配置库中查找文件或代码

    [update 2017.2.11] 最新版本的TFS 2017已经增加了代码搜索功能,可以参考这个链接 https://blogs.msdn.microsoft.com/visualstudioal ...

  4. 50个实用的jQuery代码段让你成为更好的Web前端工程师

    本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...

  5. spring和struts2的整合的xml代码

    导入spring的pring-framework-4.0.4.RELEASE的所有包,导入struts2下(对于初学的推荐)bin下所有的包,虽然有些包可以能现在你用不到,但可以保证你基本上不会出现缺 ...

  6. windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

    为什么使用PHP_CodeSniffer 一个开发团队统一的编码风格,有助于他人对代码的理解和维护,对于大项目来说尤其重要. PHP_CodeSniffer是PEAR中的一个用PHP5写的用来检查嗅探 ...

  7. 经验分享:10个简单实用的 jQuery 代码片段

    尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...

  8. 【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题

    $.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...

  9. Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构

    分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...

随机推荐

  1. 26 About the go command go命令行

    About the go command  go命令行 Motivation Configuration versus convention Go's conventions Getting star ...

  2. rsync + inotify 实时同步

    1. 前言 2 台 nginx 需要做集群, 静态文件和php文件都在nginx服务器本地. 有三种方案: (1)NFS (2)Rsync + inotify (3)共享存储服务器 第一种:当 nfs ...

  3. No.11 selenium学习之路之浏览器大小

    通过set_window_size()方法可以设置打开的浏览器大小 maximize_window()方法可以把当前浏览器最大化 例子:

  4. 5 个非常有用的 Laravel Blade 指令,你用过哪些?

    接下来我将带大家认识下五个 Laravel Blade 指令,这些指令将让你在解决特定问题时如虎添翼.如果你是刚接触 Laravel 的用户,这些小技巧能带你认识到 Laravel Blade 模板引 ...

  5. ZOJ 3537 Cake(凸包+区间DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537 题目大意:给出一些点表示多边形顶点的位置,如果不是凸多边形 ...

  6. 多线程 or I/O复用select/epoll

    1:多线程模型适用于处理短连接,且连接的打开关闭非常频繁的情形,但不适合处理长连接.线程模型默认情况下,在Linux下每个线程会开8M的栈空间,在TCP长连接的情况下,以2000/分钟的请求为例,几乎 ...

  7. PL/SQL开发中动态SQL的使用方法

    一般的PL/SQL程序设计中,在DML和事务控制的语句中可以直接使用SQL,但是DDL语句及系统控制语句却不能在PL/SQL中直接使用,要想实现在PL/SQL中使用DDL语句及系统控制语句,可以通过使 ...

  8. Kafka压力测试(自带测试脚本)(单机版)

    一.测试目的 本次性能测试在正式环境下单台服务器上Kafka处理MQ消息能力进行压力测试.测试包括对Kafka写入MQ消息和消费MQ消息进行压力测试,根据10w.100w和1000w级别的消息处理结果 ...

  9. 《精通Python设计模式》学习行为型之责任链模式

    感觉是全新的学习了. 因为在以前的工作中,并没有有意识的去运用哪一种编程模式. 以后要注意的了. 这才是高手之路呀~ class Event: def __init__(self, name): se ...

  10. sicily 1198. Substring (递归全排列+排序)

    DescriptionDr lee cuts a string S into N pieces,s[1],…,s[N]. Now, Dr lee gives you these N sub-strin ...