search
Homephp教程php手册PHP DOMDocument实现XML读写操作

在php中操作xml文档我们可以直接调用DOMDocument类或使用simplexml_load_string类来操作,这些都不需要加载算是内置函数了,我们直接使用.

PHP脚本读取XML最原始,最笨的方法,代码如下:

$doc = new DOMDocument();  
$doc->load('test.xml');  
$rows = $doc->getElementsByTagName("ds");  
$d = array();  
$i = 0;  
foreach($rows as $row)  
{  
   $web     = $row->getElementsByTagName('web');  
   $webUrl  = $row->getElementsByTagName('webUrl');  
   $d[$i]['web']    = $web->item(0)->nodeValue;  
   $d[$i]['webUrl'] = $webUrl->item(0)->nodeValue;  
   $i++;  
}  
print_R($d);

简单,容易,速度快,代码如下:

$xmlData = file_get_contents('test.xml');  
$xml = simplexml_load_string($xmlData);  
$xmlArr = objectToArray($xml);

objectToArray函数代码如下:

function objectToArray($object)  
{    
   if(!$object) return '';  
   $result = array();    
   $object = is_object($object) ? get_object_vars($object) : $object;    
   foreach ($object as $key => $val) {    
       $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val;    
       $result[$key] = $val;    
   }    
   return $result;    
}

上面讲到了读取xml,下面再介绍写xml实例,代码如下:

$arr = array(  
    array('id'=>1,'web'=>'好脚本','webUrl'=>'http://www.phprm.com'),  
    array('id'=>2,'web'=>'PHP脚本','webUrl'=>'http://www.phprm.com/'),  
    array('id'=>3,'web'=>'JavaScript脚本','webUrl'=>'http://www.phprm.com/'),  
    array('id'=>4,'web'=>'js脚本','webUrl'=>'http://www.phprm.com/'),  
    array('id'=>5,'web'=>'PHP脚本示例','webUrl'=>'http://www.phprm.com/'),  
    array('id'=>5,'web'=>'JavaScript脚本示例','webUrl'=>'http://www.phprm.com/')  
);  
/*使用dom生成xml,注意生成的xml中会没有空格。*/ 
$dom=new DOMDocument('1.0','utf-8');  
$path= "logs/test.xml";  
$data=$dom->createElement('data');  
$dom->appendChild($data);  
foreach($arr as $v)  
{  
    $ds = $dom->createElement('ds');  
    $id = $dom->createAttribute('id');   
    $id->nodeValue = $v['id'];  
    $ds->setAttributeNode($id);  
    $data->appendChild($ds);  
    foreach($v as $kk=>$vv)  
    {  
       ${$kk} = $dom->createElement($kk);   
       $value= $dom->createTextNode($vv);  
       ${$kk}->appendChild($value);  
       $ds->appendChild(${$kk});  
    }  
}  
$dom->saveXML();  
$dom->save($path);


教程链接:

随意转载~但请保留教程地址★

Statement
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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.