最近因为有需要,对生成xml文件进行了一些处理,很早之前使用过SimpleXML加载接口返回的xml数据并处理,这次的使用偏向于XML文件的生成。有一个需求,生成的xml文件格式需要格式化好,xml文件在浏览器里看到的样子,一行一行内容,并且不同级别间有内容的缩进控制。这是浏览器自身的功能,实际如果不处理,生成的XML文件字符串间是连续的。例如:
<?xml version="1.0" encoding="utf-8"?>
<student><item><name>liming</name><age>21</age><sex>man</sex><note>喜欢看书</note></item><item><name>lucy</name><age>19</age><sex>woman</sex><note>喜欢看电影</note></item><item><name>lily</name><age>19</age><sex>woman</sex><note>没什么喜欢的</note></item></student>
这样的格式在linux服务器以及在编辑器中查看都特别不好看。于是找实现这种格式化缩进的处理方法。我使用的是DomDocument来生成XML文件,DomDocument是PHP默认支持的类扩展,其中创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法。其有一项很重要的属性: formatOutput 布尔类型. Nicely formats output with indentation and extra space.
在创建DomDocument类的时候设置这个属性为true,则在生成的xml文件会自动采用缩进格式,非常方便。封装的类文件及试验类的下载地址:php生成缩进格式的xml文件的封装类及测试 类代码文件Xmlmake.php代码如下:
/*
* Note:生成XML处理类
* Author:www.04007.cn
* Date:2017-10-23
*/
class Xmlmake
{
//静态例属性.
public static $document = null;
//实例化类
public function __construct($version='1.0', $charset='utf-8')
{
//初始化DomDocument类.并单例化
if(!self::$document)
{
self::$document = new DomDocument($version, $charset);
self::$document->formatOutput = true;
self::$document->preserveWhiteSpace = false;
}
}
//创建元素
public function createElement($element)
{
return self::$document->createElement($element);
}
//添加子节点
public function appendchild($child)
{
return self::$document->appendchild($child);
}
//返回xml字符串
public function saveXML()
{
return self::$document->saveXML();
}
//添加Item
public function createItem($item, $data, $attribute=array())
{
if(is_array($data))
{
foreach ($data as $key => $val)
{
//创建元素,元素名称不能以数字开头.
is_numeric($key{0}) && exit($key. ' error:first char cannot be a number.');
//添加元素
$temp = self::$document->createElement($key);
$item->appendchild($temp);
//添加元素值
$text = self::$document->createTextNode($val);
$temp->appendchild($text);
if(isset($attribute[$key]))
{
//如果此字段存在相关属性需要设置
foreach($attribute[$key] as $akey => $row)
{
//创建属性节点
$temps = self::$document->createAttribute($akey);
$temp->appendchild($temps);
//创建属性值节点
$aval = self::$document->createTextNode($row);
$temps->appendChild($aval);
}
}
}
}
}
//加载Xml文件
public function loadFile($fpath)
{
if(!is_file($fpath))
{
exit($fpath .' is a invalid file.');
}
//成功时返回 TRUE, 或者在失败时返回 FALSE
return self::$document->load($fpath);
}
//保存XML至某人文件.
public function saveFile($fpath)
{
//执行文件写入
return file_put_contents($fpath, self::$document->saveXML());
}
}
测试调用的PHP程序如下:require 'Xmlmake.php';
$Xmlmake = new Xmlmake();
$data = array(
'liming'=>array('name'=>'liming','age'=>21,'sex'=>'man','note'=>'喜欢看书'),
'lucy'=>array('name'=>'lucy','age'=>19,'sex'=>'woman','note'=>'喜欢看电影'),
'lily'=>array('name'=>'lily','age'=>19,'sex'=>'woman','note'=>'没什么喜欢的'),
);
$Xml = $Xmlmake->createElement('student');
$Xmlmake->appendchild($Xml);
foreach ($data as $student)
{
$item = $Xmlmake->createElement('item');
$Xml->appendchild($item);
$Xmlmake->createItem($item, $student);
}
$Xmlmake->saveFile('student.xml');
echo $Xmlmake->saveXML();
//加载xml内容.
$loadRs = $Xmlmake->loadFile('student.xml');
执行后将生成格式整洁的XML文件,student.xml。格式截图如下:
友情提示:垃圾评论一律封号...