Heim  >  Artikel  >  Backend-Entwicklung  >  php基于dom实现的图书xml格式数据示例_php技巧

php基于dom实现的图书xml格式数据示例_php技巧

PHP中文网
PHP中文网Original
2016-05-16 19:13:201353Durchsuche

这篇文章主要介绍了php基于dom实现的图书xml格式数据,结合实例形式分析了php数组转换xml格式数据的相关操作技巧,需要的朋友可以参考下

本文实例讲述了php基于dom实现的图书xml格式数据。分享给大家供大家参考,具体如下:


<?php
 $books = array();
 $books [] = array(
 &#39;title&#39; => &#39;PHP Hacks&#39;,
 &#39;author&#39; => &#39;Jack Herrington&#39;,
 &#39;publisher&#39; => "O&#39;Reilly"
 );
 $books [] = array(
 &#39;title&#39; => &#39;Podcasting Hacks&#39;,
 &#39;author&#39; => &#39;Jack Herrington&#39;,
 &#39;publisher&#39; => "O&#39;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[&#39;author&#39;] )
 );
 $b->appendChild( $author );
 $title = $doc->createElement( "title" );
 $title->appendChild(
 $doc->createTextNode( $book[&#39;title&#39;] )
 );
 $b->appendChild( $title );
 $publisher = $doc->createElement( "publisher" );
 $publisher->appendChild(
 $doc->createTextNode( $book[&#39;publisher&#39;] )
 );
 $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&#39;Reilly</publisher>
 </book>
 <book>
  <author>Jack Herrington</author>
  <title>Podcasting Hacks</title>
  <publisher>O&#39;Reilly</publisher>
 </book>
</books>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn