工作中遇到用PHP处理XML的相关东西。刚刚学PHP,代码中有很多值得优化的地方。
主要运用simpleXML处理。写了一个通用化的XML处理代码。
Xmlcreater.php
<?php
/**
* @author: Even
* @version: 1.0.1
* @date: 2017.5.17
*/
define('LOAD_XMLFILE_ERROR',-1);
define('FILE_IO_ERROR', -2);
define('XML_HEADER','<?xml version="1.0" encoding="utf-8" ?>');
class Xmlcreater{
protected $m_arrayXMLData = null;
protected $m_errorMessage = null;
protected $m_objXML = null; /*This is only part of the XML input object. */
protected $m_rootName = '';
protected $m_xmlFilePath = '';
//protected $m_xmlString = ''; /*This is only part of the XML output string.*/
protected $m_xmlFileObj = null;
/*There must be a parameter for the time being.*/
public function __construct(){
$argNum = func_num_args();
switch($argNum){
case 0:
$this->m_xmlFilePath = '';
break;
case 1:
$this->m_xmlFilePath = func_get_arg(0);
break;
}
/*Load XML File.*/
try{
if (!file_exists($this->m_xmlFilePath)) {
$this->m_xmlFileObj = fopen($this->m_xmlFilePath,'w+');
fwrite($this->m_xmlFileObj,'<a> </a>');
fclose($this->m_xmlFileObj);
$this->m_xmlFileObj = null;
}
libxml_disable_entity_loader(false);
$this->m_objXML = simplexml_load_file($this->m_xmlFilePath,'SimpleXMLElement', LIBXML_NOCDATA);
}
catch(Exception $e){
$this->m_errorMessage = LOAD_XMLFILE_ERROR;
}
}
public function __destruct() {
if(null != $this->m_xmlFileObj){
fclose($this->m_xmlFileObj);
$this->m_xmlFileObj = null;
}
}
/*Get Data From Database For Create XML File.*/
public function getData(){
$this->m_arrayXMLData = $this->xmlToArray($this->m_objXML);
return $this->m_arrayXMLData;
}
public function getRootName(){
return $this->m_rootName;
}
public function setRootName($strName){
$this->m_rootName = $strName;
}
/**
* @param: Source Data Array, XML Root Name, The XML Object.
* @return String: XML String.
* @summary:
* The #3 Parament Only For Recursive Functions, We Can Use $dataArray And $rootName When We Call This Function.
*
*/
public function arrayToXML($dataArray, $rootName, &$xmlObj = null ){
$ptrArray = null;
$ptrElement = null;
/*Create XML Object.*/
if(null == $xmlObj){
$tempXml = XML_HEADER.'<'.$rootName.'></'.$rootName.'>';
$xmlObj = simplexml_load_string($tempXml);
}
if(null != $rootName){
$dataArray = $dataArray[$rootName];
}
/*Add Attributes.*/
if(array_key_exists('@attributes',$dataArray)) {
foreach ($dataArray['@attributes'] as $keyAttr => $valueAttr) {
$xmlObj->addAttribute($keyAttr,$valueAttr);
}
}
/*Add Element.*/
//CHECKIT: if(is_array($valueEle));
foreach ($dataArray as $keyEle => $valueEle) {
if($keyEle != '@attributes'){
if(is_array($valueEle)){
$ptrArray = $valueEle;
$ptrElement = $xmlObj->addChild($keyEle);
}
else{
$ptrElement = $xmlObj->addChild($keyEle,$valueEle);
}
$this->arrayToXML($ptrArray,null,$ptrElement);
}
}/*End of foreach.*/
return $xmlObj->saveXML();
} /*End Of Function arrayToXML.*/
/**
* @param: The XML String Or Object.
* @return: XML Object.
*
*/
private function checkValueType(&$xml){
$valueType = gettype($xml);
switch($valueType){
case 'string':
return simplexml_load_string($xml);
case 'object':
return $xml;
default:
return null;
}
}
/**
* @param: Simple XML Object / XML String.
* @return:
* a.An Array Of XML Data.
* b.If XML String Is Not A Standard XML, Return Null Or Generate An Exception.
* @summary:
* XML Object To Array Value.
*
*/
public function xmlToArray($xml){
$xml = $this->checkValueType($xml);
$this->m_arrayXMLData = json_decode(json_encode($xml),true);
return $this->m_arrayXMLData;
} /*End Of Function xmlToArray.*/
public function createXmlFile($fileInfo, $xmlString){
$tempRes = null;
try{
/*Create File.*/
$this->m_xmlFileObj = fopen($fileInfo,"w");
//chmod($this->m_xmlFileObj, 0757);
/*Write File.*/
$tempRes = fwrite($this->m_xmlFileObj,$xmlString);
return $tempRes;
}
catch(Exception $e){
return FILE_IO_ERROR;
}
}
public function getXmlString($simplexmlObj){
return $simplexmlObj->saveXML();
}
public function getXmlObject(