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

PHP中文网
Libérer: 2016-05-16 19:13:20
original
1342 Les gens l'ont consulté

这篇文章主要介绍了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();
?>
Copier après la connexion


运行结果如下:


<?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>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!