- 相關(guān)推薦
php數(shù)組基于dom實(shí)現(xiàn)轉(zhuǎn)換xml格式數(shù)據(jù)
導(dǎo)語:下面小編要給大家提供的是php數(shù)組基于dom實(shí)現(xiàn)轉(zhuǎn)換xml格式數(shù)據(jù),大家可以參考閱讀,更多詳情請(qǐng)關(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();
?>
運(yùn)行結(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實(shí)現(xiàn)轉(zhuǎn)換xml格式數(shù)據(jù)】相關(guān)文章:
PHP 數(shù)組和字符串互相轉(zhuǎn)換實(shí)現(xiàn)方法06-28
PHP如何使用DOM和simplexml讀取xml文檔07-22
php字符串與數(shù)組怎么轉(zhuǎn)換10-04
PHP數(shù)據(jù)類型之?dāng)?shù)組變量詳解10-04
PHP如何使用curl實(shí)現(xiàn)數(shù)據(jù)抓取09-27
php實(shí)現(xiàn)插入數(shù)組但不影響原有順序的方法09-05
如何實(shí)現(xiàn)PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本09-23