pear中几个实用的xml代码库
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代码库的更多相关文章
- 在Jenkins中使用Git Plugin访问Https代码库失败的问题
最近需要在Jenkins上配置一个Job,SCM源是http://git.opendaylight.org/gerrit/p/integration.git 于是使用Jenkins的Git Plugi ...
- 分享实用的JavaScript代码库
1 var keyCodeMap = { 2 8: 'Backspace', 3 9: 'Tab', 4 13: 'Enter', 5 16: 'Shift', 6 17: 'Ctrl', 7 18: ...
- 在Team Foundation Server (TFS)的代码库或配置库中查找文件或代码
[update 2017.2.11] 最新版本的TFS 2017已经增加了代码搜索功能,可以参考这个链接 https://blogs.msdn.microsoft.com/visualstudioal ...
- 50个实用的jQuery代码段让你成为更好的Web前端工程师
本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...
- spring和struts2的整合的xml代码
导入spring的pring-framework-4.0.4.RELEASE的所有包,导入struts2下(对于初学的推荐)bin下所有的包,虽然有些包可以能现在你用不到,但可以保证你基本上不会出现缺 ...
- windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码
为什么使用PHP_CodeSniffer 一个开发团队统一的编码风格,有助于他人对代码的理解和维护,对于大项目来说尤其重要. PHP_CodeSniffer是PEAR中的一个用PHP5写的用来检查嗅探 ...
- 经验分享:10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...
- 【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题
$.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...
- Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构
分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...
随机推荐
- 读sru代码
1. def read_corpus(path, eos="</s>"): data = [ ] with open(path) as fin: for line in ...
- HTTP::Request 用 add-content 添加 POST参数
sub MAIN($cmd) { use HTTP::UserAgent; my $r = HTTP::Request.new(); $r.uri: 'http://localhost/a.php'; ...
- unity 欧拉旋转
欧拉旋转 在文章开头关于欧拉旋转的细节没有解释的太清楚,而又有不少人询问相关问题,我尽量把自己的理解写到这里,如有不对还望指出. 欧拉旋转是怎么运作的 欧拉旋转是我们最容易理解的一 ...
- python基础-类的属性(类属性,实例属性,私有属性)
一:类的属性 类的属性分为:类属性(公有属性),实例属性和私有属性. 1)类属性(公有属性(静态字段): 类定义时直接指定的属性(不是在__init__方法中),可以通过类名直接访问属性,并且保存 ...
- vue项目下使用iview总结
iview在IE浏览器下有问题,打开页面是空白
- python之uinttest,用例执行顺序
unittest单元测试框架, 以test开头的测试用例,默认执行顺序是按照ASC码来执行 如果有类,先排序执行类,在执行类中,再排序用例顺序执行 如果想要按照指定的顺序执行测试用例. 那么就需要用到 ...
- AdvStringGrid 点击标题头 自动排序
- VFS,super_block,inode,dentry—结构体图解
总结: VFS只存在于内存中,它在系统启动时被创建,系统关闭时注销. VFS的作用就是屏蔽各类文件系统的差异,给用户.应用程序.甚至Linux其他管理模块提供统一的接口集合. 管理VFS数据结构的组成 ...
- **PHP转义Json里的特殊字符的函数
http://www.banghui.org/11332.html 在给一个 App 做 API,从服务器端的 MySQL 取出数据,然后生成 JSON.数据中有个字段叫 content,里面保存了文 ...
- 关于CSRF的那点事儿
0x01 CSRF简介 CSRF,也称XSRF,即跨站请求伪造攻击,与XSS相似,但与XSS相比更难防范,是一种广泛存在于网站中的安全漏洞,经常与XSS一起配合攻击. 0x02 CSRF原理 ...