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

PHP中文网
Freigeben: 2016-05-16 19:13:20
Original
1352 Leute haben es durchsucht

这篇文章主要介绍了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();
?>
Nach dem Login kopieren


运行结果如下:


<?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>
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!