php分析xml实例程序
<?xml version='1.0' encoding='UTF-8'?>
<root>
<key id="5201314" name="望咩掉啊">
<user id='6789' name='不上学了'><![CDATA[我靠!!!@*&^$(*%&]]></key>
<user id="6789" name="不上学了">222</key>
</key>
<key id="5201314" name="望咩掉啊">
<user id='6789' name='不上学了'><![CDATA[我靠!!!|""!@":#$@$]]></key>
<user id="6789" name="不上学了">222</key>
</key>
<key id="5201314" name="望咩掉啊">
<user id='6789' name='不上学了'><![CDATA[我靠!!!<><><><>]]></key>
<user id="6789" name="不上学了">222</key>
</key>
</root>
<?php
/* func.xml.php 2008-09-20 kilobug
必须声明头部属性,xml每个节点的值和属性严禁出现 <、>、" 字符,如需出现,请使用CDATA
每个节点属性必须统一使用单引号'或双引号"
忽略注释,返回一个数组。
*/
$xmlContent = ''; //临时变量
/* 载入xml文件,解析后返回数组 */
function loadXML( $path, $assoc = null, $attrassoc = null ) {
return parseXML( @file_get_contents( $path ), $assoc, $attrassoc );
}
/* 解析XMl内容,返回数组 */
function parseXML( $xmltext, $assoc = null, $attrassoc = null ) {
$xmldata = array();
//转换顶部属性
$noderoot = strpos( $xmltext, '<?' );
$noderootspace = strpos( $xmltext, ' ' );
$nodefoot = strpos( $xmltext, '?>' );
$attr = trim( substr($xmltext, $noderootspace, $nodefoot-$noderoot-5) );
//转换节点
$xmldata = array_merge(
array(
array_merge(
array( substr($xmltext, $noderoot+2, $noderootspace-$noderoot-2) ),
parseXMLAttrs($attr, $attrassoc)
)
),
parseXMLNodes( trim(substr($xmltext, $nodefoot+2)), $assoc, $attrassoc )
);
return $xmldata;
}
/* 解析XML节点,返回数组 */
function parseXMLNodes( $nodestr, $assoc = null, $attrassoc = null ) {
global $xmlContent;
$xmlContent = $nodestr;
if ( empty($xmlContent) || !strstr($xmlContent, '<') ) {
return array();
}
$nodedate = array();
$i = 0;
while ( !empty($xmlContent) ) {
$noderoot = strpos( $xmlContent, '<' );
$nodefoot = strpos( $xmlContent, '>' );
$nodeend = strpos( $xmlContent, '/' );
$cdataroot = strpos( $xmlContent, '<![CDATA[' );
$cdatafoot = strpos( $xmlContent, ']]>' );
if ( $noderoot === $cdataroot ) { //判断是否CDATA
$nodedate = substr($xmlContent, $cdataroot+9, $cdatafoot-$cdataroot-9);
$xmlContent = substr( $xmlContent, strpos($xmlContent, '>', $cdatafoot+3)+1 );
break $nodedate;
} else if ( $noderoot == $nodeend-1 ) { //是否结束节点,是则返回前面的内容
if ( $i == 0 ) {
$nodedate = substr( $xmlContent, 0, $noderoot );
$xmlContent = substr( $xmlContent, $nodefoot+1 );
return $nodedate;
}
else {
$xmlContent = substr( $xmlContent, $nodefoot+1 );
break;
}
}
$nodecompart = strpos( $xmlContent, ' ' );
//判断有无属性
if ( $nodecompart != false && $nodecompart > $noderoot && $nodecompart < $nodefoot ) { //有属性
$nodedate[$i][0] = array_merge( array($assoc ? substr($xmlContent, $noderoot+1, $nodecompart-$noderoot-1) : ''), parseXMLAttrs(substr($xmlContent, $nodecompart+1, $nodefoot-$nodecompart-($nodefoot-1 == $nodeend ? 2 : 1)), $attrassoc) );
} else { //无属性
$nodedate[$i][0] = array( $assoc ? substr($xmlContent, $noderoot+1, $nodefoot-$noderoot-1) : '' );
}
$i++;
$xmlContent = substr( $xmlContent, $nodefoot+1 ); //截取本节点后面内容
if ( $nodefoot-1 == $nodeend ) { //判断是否单节点
$nodedate[$i-1][1] = '';
continue;
} else {
$nodedate[$i-1][1] = parseXMLNodes( $xmlContent, $assoc, $attrassoc );
}
}
return $nodedate;
}
/* 解析XML节点属性,返回数组 */
function parseXMLAttrs( $attrstr, $assoc = null ) {
if ( !$attrstr ) return array();
echo( $attrstr . "n" );
$attrdata = array();
$attrcenter = $attrfoot = 1; //开始转换
$tmpnum = false;
while ( $attrcenter != false ) {
$tmpnum = strpos($attrstr, '='');
$attrcenter = strpos($attrstr, '="');
if ( $tmpnum == false ) {
$attrfoot = strpos($attrstr, '"', $attrcenter+2);
} else {
$attrcenter = $tmpnum;
$attrfoot = strpos($attrstr, ''', $attrcenter+2);
}
if ( $assoc ) { //是否关联数组
$attrdata[substr( $attrstr, 0, $attrcenter)] = substr( $attrstr, $attrcenter+2, $attrfoot-$attrcenter-2 );
} else {
$attrdata[] = substr( $attrstr, $attrcenter+2, $attrfoot-$attrcenter-2 );
}
$attrstr = trim(substr($attrstr, $attrfoot+1));
if ( !$attrstr ) {
break;
}
}
return $attrdata;
}
//解析数组为XML(没写)
function parseArrayToXML( $ary ) {
}
//解析数组为XML的Nodes(没写)
function parseArrayToXMLNodes( $ary ) {
}
/* 测试
$t = microtime();
//loadXML('../books.xml');
print_r( loadXML('../test.xml', 1, 1) ); //自己找一个常见的xml文档
echo microtime() - $t;
*/
?>
PHP SimpleXML
SimpleXML是什么?
SimpleXML是新的PHP 5 。这是一个简单的方法获得一个要素的属性和文字,如果您知道XML文件的布局。
相比到DOM或外籍分析器, SimpleXML只需几行代码读取文本数据从一个组成部分。
SimpleXML转换XML文档到一个对象,像这样:
内容-转换为单一属性S impleXMLElement对象。当有一个以上的元素一个层次上,他们放入一个数组
属性-访问使用关联数组,如果指数对应的属性名称
元数据-数据从文本的内容转换为字符串。如果一个要素有一个以上的文本节点,他们将被安排在该命令他们发现
SimpleXML速度快,易于使用的表演时,基本任务包括:
读XML文件
提取数据从XML字符串
编辑文本节点或属性
然而,当先进的处理XML的,如名称,你是最好的外籍人士使用分析器或XML的DOM 。
安装
从PHP 5.0 , SimpleXML职能是PHP核心的一部分。没有安装即可使用这些功能。
用SimpleXML
下面是一个XML文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
我们希望输出的元素名称和数据的XML档案以上。
这是做什么:
加载XML文件
获取的名字的第一个元素
创建一个循环,将触发每个孩子节点,使用儿童( )函数
输出元素的名称和数据,每个子节点
例如
<?php $xml = simplexml_load_file("test.xml");echo $xml->getName() . "<br />";foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } ?>输出结果为note to: Tove from: Jani heading: Reminder body: Don't forget me this weekend!转载请注明来自: www.111cn.net/phper/php.html
php读了xml很方便的,我们下面用dom与php自带的xml_parser来实例吧,我们先看看wk.xml 文件, 其实这里是blogbus的rss文件哦,
<?xml version="1.0" encoding="gb2312"?>
<rss version="2.0">
<channel>
<title>mikeowen</title>
<link>http://mikeowen.blogbus.com</link>
<description><![CDATA[Design]]></description>
<generator> by blogbus.com </generator>
<lastBuildDate>Tue, 30 Jan 2007 13:46:52 +0800</lastBuildDate>
<image>
<url>http://public.blogbus.com/images/head.gif</url>
<title>mikeowen</title>
<link>http://mikeowen.blogbus.com</link>
</image>
<item>
<title>vanke</title>
<description>tff</description>
<link>http://mikeowen.blogbus.com/logs/28560579.html</link>
<author>mikeowen</author>
<pubDate>Fri, 05 Sep 2008 12:41:22 +0800</pubDate>
</item>
<item>
<title>something3</title>
<description>eee</description>
<link>http://mikeowen.blogbus.com/logs/23972142.html</link>
<author>mikeowen</author>
<pubDate>Wed, 02 Jul 2008 12:26:40 +0800</pubDate>
</item>
</channel>
</rss>
这是我一个同事的博客rss文件我取下来作实例了吧.下面我们来看看解析xml 的方法用dom来做,
<?php
$doc = new DOMDocument();
$doc->load( 'wk.xml' );
$books = $doc->getElementsByTagName( "item" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "title" );
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "link" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "pubDate" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publishern";
}
?>
简单吧,直接读取节点然后再取当前第一个节点的值就行了.好了下面我们再看第种方法用php 自然的.
<?php
$g_books = array();
$g_elem = null;
function startElement( $parser, $name, $attrs )
{
global $g_books, $g_elem;
if ( $name == 'item' ) $g_books []= array();
$g_elem = $name;
}
function endElement( $parser, $name )
{
global $g_elem;
$g_elem = null;
}
function textData( $parser, $text )
{
global $g_books, $g_elem;
if ( $g_elem == 'link' ||
$g_elem == 'pubDate' ||
$g_elem == 'title' )
{
$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
}
}
$parser = xml_parser_create();
xml_set_element_handler( $parser, "startElement", "endElement" );
xml_set_character_data_handler( $parser, "textData" );
$f = fopen( 'wk.xml', 'r' );
while( $data = fread( $f, 4096 ) )
{
xml_parse( $parser, $data );
}
xml_parser_free( $parser );
foreach( $g_books as $book )
{
echo $book['title']." - ".$book['link']." - ";
echo $book['pubDate']."n";
}
?>
这种代码多一点,单效率要比上面那个高很多的.
MXML是一个可以让你在Adobe Flex中布局用户界面组件的一种XML语言。你也可以使用MXML声明来定义程序中的非可视化组件。安装好FLEX Development 后,你就可以轻松的结合PHP完成一个FLASH曲线图。
先从PHP入手:
FLEX 和PHP的交互最常用的就是
和XML的交互,就是用PHP生成XML,然后用FLEX去读取并且显示。
我们以XML的形式先写一个帐目详单的数据。
代码:
header("Content-Type: text/xml");
$arr = array(
array('day'=>'01','sum'=>'153.1'),
array('day'=>'02','sum'=>'882.86'),
array('day'=>'03','sum'=>'456.9'),
array('day'=>'04','sum'=>'717.7'),
array('day'=>'05','sum'=>'255.1'),
array('day'=>'06','sum'=>'533.1')
);
$xml_return .= "<bills>";
foreach ( $arr as $use){
$xml_return .="<bill><sum>{$use['sum']}</sum><name>{$use['day']}</name></bill> ";
}
$xml_return.= "</bills>";
echo $xml_return;
显示6个月的消费记录,$arr也可以是从数据库读出来的数据,这里只是作为一个例子,所以直接给一个数组赋值。
- <bills>
- <bill>
<sum>153.1</sum>
<name>01</name>
</bill>
- <bill>
<sum>882.86</sum>
<name>02</name>
</bill>
- <bill>
<sum>456.9</sum>
<name>03</name>
</bill>
- <bill>
<sum>717.7</sum>
<name>04</name>
</bill>
- <bill>
<sum>255.1</sum>
<name>05</name>
</bill>
- <bill>
<sum>533.1</sum>
<name>06</name>
</bill>
</bills>
Php部分就完成了 接下来是写MXML:
代码:
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="getaccount()">
<mx:HTTPService id="listRequest" url="" useProxy="false" method="POST">
<mx:request xmlns=""> </mx:request>
</mx:HTTPService>
<mx:Script><![CDATA[
import mx.rpc.events.ResultEvent;//引用ResultEvent类
public function getaccount():void{
listRequest.url='http://localhost/arr.php';
listRequest.send();
}
]]></mx:Script>
<mx:LineChart showDataTips="true" clipContent="false" x="126" y="10" id="tongjitu" dataProvider="{listRequest.lastResult.bills.bill}" width="678">
<mx:horizontalAxis>
<mx:CategoryAxis dataProvider="{listRequest.lastResult.bills.bill}" categoryField="name" title="(日)" />
</mx:horizontalAxis>
<mx:series>
<mx:LineSeries displayName="每日消费" yField="sum" xField="name"/>
</mx:series>
</mx:LineChart>
</mx:Application>
逐一解释一下
代码:
Application //MXML应用程序开头
代码:
creationComplete="getaccount()" //代表页面加载完成后 执行 getaccount函数
代码:
〈mx:HTTPService id="listRequest" url="" useProxy="false" method="POST"〉
〈mx:request xmlns=""〉 〈/mx:request〉
〈/mx:HTTPService〉
//则是一个HTTP的请求,发送POST到一个PHP文件 去获取数据
代码:
mx:Script //MXML里面的脚本 都用 <mx:Script>包含进去
下面分析下getaccount函数:
代码:
public function getaccount():void{
listRequest.url='http://localhost/arr.php';
listRequest.send();
}
//列出了 HTTPService 请求地址是 http://localhost/arr.php
//你同样也可以写成相对路径 比如 listRequest.url='./arr.php';
代码:
mx:LineChart //是一个 曲线 组件,我们可以直接拿来使用
代码:
dataProvider :数据源:listRequest.lastResult.bills.bill
bills.bill 是XML里面的标签
代码:
mx:LineSeries displayName="每日消费" yField="sum" xField="name"
//则定义了 横纵坐标的值。
//Sum name 就是XML里面的数据的标签
<?php
$doc = new DOMDocument();
$doc->load( 'books.xml' );
$books = $doc->getElementsByTagName( "book" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publishern";
}
?>
相关文章
- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
- 本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
- 这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
- 大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
- 这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文
时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25- 本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
- 这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
- 这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
- 这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
- 因此,正确的原子操作是真正被执行过的。是物理执行。在当前事务中确实能看到插入的记录。最后只不过删除了。但是AUTO_INCREMENT不会应删除而改变值。1、为什么auto_increament没有回滚?因为innodb的auto_increament的...2014-05-31
- 这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
- 这篇文章主要为大家详细介绍了微信小程序实现点击导航条切换页面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-19
- 这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
- 这篇文章主要为大家详细介绍了微信小程序实现canvas分享朋友圈海报,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-21
- 这篇文章主要介绍了Python爬取微信小程序通用方法代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-29
- 这篇文章主要介绍了微信小程序 页面跳转传递值几种方法详解的相关资料,需要的朋友可以参考下...2017-01-16
- 这篇文章主要介绍了微信小程序自定义底部弹出框动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-16