php的XML檔案解釋類應用執行個體,phpxml類應用執行個體
本文執行個體講述了php的XML檔案解釋類及其用法,是非常實用的技巧。分享給大家供大家參考。具體如下:
XMLParser.class.php類檔案如下:
<?php /** XML 檔案分析類 * Date: 2013-02-01 * Author: fdipzone * Ver: 1.0 * * func: * loadXmlFile($xmlfile) 讀入xml檔案輸出Array * loadXmlString($xmlstring) 讀入xmlstring 輸出Array */ class XMLParser{ /** 讀取xml檔案 * @param String $xmlfile * @return Array */ public function loadXmlFile($xmlfile){ // get xmlfile content $xmlstring = file_exists($xmlfile)? file_get_contents($xmlfile) : ''; // parser xml list($flag, $data) = $this->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樣本程式如下:
<?php require "XMLParser.class.php"; $xmlfile = 'file.xml'; $xmlstring = '<?xml version="1.0" encoding="utf-8"?> 1000 100 fdipzone 1 28 '; echo ''; $xml_parser = new XMLParser(); echo "response xmlfile\r\n"; list($flag, $xmldata) = $xml_parser->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檔案
#使用dom產生xml,注意產生的xml中會沒有空格。
$dom=new DOMDocument('1.0','utf-8');
$path="test.xml"; // $path 為xml檔案的儲存路徑。
$module=$dom->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(' 表面採用進口楸木木皮拼貼成精美的拼花,自然清晰的木材紋理得到完美的呈現,各種材質的合理搭配締造了雅意系列精緻的傢具產品。 <br /> ');
$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檔案解釋類及其用法,是非常實用的技巧。分享給大家供大家參考。具...