php的xml文件解释类应用实例,phpxml类应用实例本文实例讲述了php的xml文件解释类及其用法,是非常实用的技巧。分享给大家供大家参考。具体如下:
xmlparser.class.php类文件如下:
parser($xmlstring); return $this->response($flag, $data); } /** 读取xmlstring * @param string $xmlstring * @return array */ public function loadxmlstring($xmlstring){ // parser xml list($flag, $data) = $this->parser($xmlstring); return $this->response($flag, $data); } /** 解释xml内容 * @param string $xmlstring * @return array */ private function parser($xmlstring){ $flag = false; $data = array(); // check xml format if($this->checkxmlformat($xmlstring)){ $flag = true; // xml to object $data = simplexml_load_string($xmlstring, 'simplexmlelement', libxml_nocdata); // object to array $this->objecttoarray($data); } return array($flag, $data); } /** 检查xml格式是否正确 * @param string $xmlstring * @return boolean */ private function checkxmlformat($xmlstring){ if($xmlstring==''){ return false; } $xml_parser_obj = xml_parser_create(); if(xml_parse_into_struct($xml_parser_obj, $xmlstring, $vals, $indexs)===1){ // 1:success 0:fail return true; }else{ return false; } } /** object 转 array * @param object $object * @return array */ private function objecttoarray(&$object){ $object = (array)$object; foreach($object as $key => $value){ if($value==''){ $object[$key] = ; }else{ if(is_object($value) || is_array($value)){ $this->objecttoarray($value); $object[$key] = $value; } } } } /** 输出返回 * @param boolean $flag true:false * @param array $data 转换后的数据 * @return array */ private function response($flag=false, $data=array()){ return array($flag, $data); } } ?>
demo示例程序如下:
loadxmlfile($xmlfile); if($flag){ print_r($xmldata); } echo response xmlstring\r\n; list($flag, $xmldata) = $xml_parser->loadxmlstring($xmlstring); if($flag){ print_r($xmldata); } echo '
'; ?>
关于php的xml预定义常量可参考官方文档:
http://www.php.net/manual/en/libxml.constants.php
希望本文所述对大家php程序设计的学习有所帮助。
php怎生成xml文件
createelement('newmodule');// root node
$dom->appendchild($module);
$year=$dom->createelement('year'); // add attribute node
$name=$dom->createattribute('name');
$name->nodevalue=最新动态;
$year->setattributenode($name);
$module->appendchild($year);
$news=$dom->createelement('news');
$year->appendchild($news);
$date=$dom->createelement('date');
$date_value=$dom->createtextnode('01-24');
$date->appendchild($date_value);
$news->appendchild($date);
$title=$dom->createelement('title');
$title_value=$dom->createtextnode('');
$title->appendchild($title_value);
$news->appendchild($title);
$info=$dom->createelement('info');
$info_value=$dom->createtextnode(' ]]>');
$info->appendchild($info_value);
$news->appendchild($info);
echo $dom->savexml();
$dom->save($path);
?>
读取php文件中的xml内容
dim xml,objnode,objatr,ncntchd,ncntatr
set xml=server.createobject(microsoft.xmldom)
xml.async=false
xml.load(server.mappath(test.xml))
set objnode=xml.documentelement
ncntchd=objnode.childnodes.length-1
'这个可以定义asp读取xml文件的那一个值,通过传递这个值来确定读取的数据
for i=0 to ncntchd
set objatr=objnode.childnodes.item(i)
ncntatr=objatr.attributes.length-1
'历遍一条记录里面的所有的记录项,记录是从0开始的
for j=0 to ncntatr
response.write objatr.attributes.item(j).text&
next
response.write
next
set objatr=nothing
set objnode=nothing
set xml=nothing
%>
参考资料:homepage.yesky.com/369/3074869.shtml
http://www.bkjia.com/phpjc/882903.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/882903.htmltecharticlephp的xml文件解释类应用实例,phpxml类应用实例 本文实例讲述了php的xml文件解释类及其用法,是非常实用的技巧。分享给大家供大家参考。具...