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

PHP中文网
Release: 2016-05-16 19:13:20
Original
1341 people have browsed it

这篇文章主要介绍了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();
?>
Copy after login


运行结果如下:


<?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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!