Home php教程 php手册 轻量级的xml文档生成类

轻量级的xml文档生成类

Jun 21, 2016 am 09:09 AM
gt quot this xml

xml

PHP代码:--------------------------------------------------------------------------------

/**
* 轻量级XML文档生成类(非dom)
* author: q3boy
* version: v0.1 aplha
* update: 2003/9/8
* 支持Element/CDATA/Declare/attribute/Comment,可选择是否包含换行和缩进
*/
class xml {

/** 元素名 */
var $name;
/** 元素值 */
var $value;
/** 元素类型 */
var $type;
/** 元素属性 */
var $attrib;
/** XML声明 */
var $declare;
/** 是否缩进换行 */
var $space;

/** 构造函数 */
function xml($name='',$value='') {
$this->name = $name;
$this->value = $value;
$this->declare = array();
$this->setTypes('Element');
$this->setAttrib(array());
$this->setSpace(false);
}

/** 设置元素类型 */
function setTypes($type) {
$this->type = $type;
}

/** 设置是否缩进换行 */
function setSpace($space) {
$this->space = $space;
}

/** 设置元素属性 */
function setAttrib($name,$value='') {
if(is_array($name)) {
$this->attrib = array_merge($this->attrib,$name);
}else {
$this->attrib[$name] = $value;
}
}

/** 添加子元素 */
function &addElement($name='',$value='') {
if(!is_array($this->value)) {
$this->value = array();
}
$xml = new xml($name,$value);
$xml->setSpace($this->space);
$this->value[] = &$xml;
return $this->value[sizeof($this->value)-1];
}

/** 添加CDATA数据 */
function &addCDATA($name='',$value='') {
if(!is_array($this->value)) {
$this->value = array();
}
$xml = new xml($name,$value);
$xml->setSpace($this->space);
$xml->setTypes('CDATA');
$this->value[] = &$xml;
return $this->value[sizeof($this->value)-1];
}

/** 添加XML声明 */
function &addDeclare($name='',$value='') {
if(!is_array($this->declare)) {
$this->value = array();
}
$xml = new xml($name,$value);
$xml->setSpace($this->space);
$xml->setTypes('Declare');
$this->declare[] = &$xml;
return $this->declare[sizeof($this->value)-1];
}

/** 添加注释文本 */
function &addComment($content='') {
if(!is_array($this->value)) {
$this->value = array();
}
$xml = new xml($content);
$xml->setSpace($this->space);
$xml->setTypes('Comment');
$this->value[] = &$xml;
return $this->value[sizeof($this->value)-1];
}

/** 返回xml文本流 */
function toString($itm='',$layer=0) {
if(!is_object($itm))$itm = &$this;
/* 换行/缩进 */
if($this->space) {
$tab = str_repeat(" ",$layer);
$tab1 = str_repeat(" ",$layer+1);
$br = "\n";
}
/* XML声明 */
for($i=0; $ideclare); $i++) {
$out = "".$itm->declare[$i]->name;
foreach($itm->declare[$i]->attrib as $key=>$val) {
$out .=" $key=\"".$this->encode($val)."\"";
}
$out.="?>$br";
}
/* 文档树 */
switch($itm->type) {
case 'CDATA':
case 'Element':
$out .= $tab.'name;
foreach($itm->attrib as $key=>$val) {
$out .=" $key=\"".$this->encode($val)."\"";
}
if(is_array($itm->value)) {
$out .='>'.$br;
for($i=0; $ivalue); $i++) {
$out .=$this->toString(&$itm->value[$i],$layer+1);
}
$out .= $tab.''.$itm->name.'>'.$br;

}elseif($itm->value!='') {
$out .='>'.$br.$this->encode($itm->value,$itm->type,$tab1,$br).$br.$tab.''.$itm->name.'>'.$br;
}else {
$out .=' />'.$br;
}
break;
case 'Comment':
$out .= ''.$br;
break;
}
return $out;
}

/** 生成xml文件 */
function toFile($file) {
$fp = fopen($file,'w');
fwrite($fp,trim($this->toString()));
fclose($fp);
}

/** 实体引用转换 */
function encode($content,$type='Element',$tab1='',$br='') {
if($type=='Element') {
return $tab1.strtr($content,array('>'=>''>','&'=>'&','"'=>'"',"'"=>'''));
}elseif($type=='CDATA') {
return '',']] >',$content).$br.']]>';
}
}
}


/* example */

/* 对象初始化 */
$xml = new xml('test');

/* 允许输出换行/缩进 */
$xml->setSpace(true);

/* 设置xml声明 */
$d = &$xml->addDeclare('xml');
$d->setAttrib("version","1.0");

/* 设置xml文档树 */
$xml1 = &$xml->addElement('test1','test1-1');
$xml1->addElement('test2','test2-1');
$x2 = &$xml1->addElement('test3','test2-2');
$x2->setAttrib("asd","1&234'\"23");

$xml1->addElement('test4','test2-3');
$xml->addElement('test455','taadsfafdsadest2-3');
$xml->addComment('adsfadsf');//注释
/* CDATA数据 */
$xml->addCDATA('cdname','dflkgmsglsd
f]gl
sdgl
asgl
sf"&ldgsldkfg]]>
sldf
gsdfgsD?FG>SS?D>');
$x1 = &$xml->addElement('test455');
$x1->setAttrib("asd",123423);
$xml->setAttrib(array("asd"=>123,'sdfgdfg'=>2341));
$xml->setAttrib("asd",123423);
/* 输出文件 */
$xml->tofile('aaa.xml');
?>



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

See all articles