- 相關推薦
php數(shù)組基于dom實現(xiàn)轉換xml格式數(shù)據(jù)
導語:下面小編要給大家提供的是php數(shù)組基于dom實現(xiàn)轉換xml格式數(shù)據(jù),大家可以參考閱讀,更多詳情請關注應屆畢業(yè)生考試網。
<?php
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "author" );
$author->appendChild(
$doc->createTextNode( $book['author'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $book['title'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "publisher" );
$publisher->appendChild(
$doc->createTextNode( $book['publisher'] )
);
$b->appendChild( $publisher );
$r->appendChild( $b );
}
echo $doc->saveXML();
?>
運行結果如下:
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
【php數(shù)組基于dom實現(xiàn)轉換xml格式數(shù)據(jù)】相關文章:
php如何基于dom實現(xiàn)圖書xml格式數(shù)據(jù)08-08
PHP將XML轉為數(shù)組的方法06-18
PHP 數(shù)組和字符串互相轉換實現(xiàn)方法06-28
PHP數(shù)組和字符串互相轉換實現(xiàn)方法09-18
用PHP數(shù)組和字符串互相轉換實現(xiàn)方法09-21
PHP如何使用DOM和simplexml讀取xml文檔07-22
php字符串與數(shù)組怎么轉換10-04
php數(shù)據(jù)類型轉換詳解10-12