- 相關(guān)推薦
php數(shù)組基于dom實現(xiàn)轉(zhuǎn)換xml格式數(shù)據(jù)
導(dǎo)語:下面小編要給大家提供的是php數(shù)組基于dom實現(xiàn)轉(zhuǎn)換xml格式數(shù)據(jù),大家可以參考閱讀,更多詳情請關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
<?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();
?>
運行結(jié)果如下:
<?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)轉(zhuǎn)換xml格式數(shù)據(jù)】相關(guān)文章:
php如何基于dom實現(xiàn)圖書xml格式數(shù)據(jù)08-08
PHP將XML轉(zhuǎn)為數(shù)組的方法06-18
PHP 數(shù)組和字符串互相轉(zhuǎn)換實現(xiàn)方法06-28
PHP數(shù)組和字符串互相轉(zhuǎn)換實現(xiàn)方法09-18
用PHP數(shù)組和字符串互相轉(zhuǎn)換實現(xiàn)方法09-21
PHP將XML轉(zhuǎn)為數(shù)組的方法詳解07-27
PHP如何使用DOM和simplexml讀取xml文檔07-22