php语言

php数组基于dom实现转换xml格式数据

时间:2024-08-27 13:13:11 php语言 我要投稿
  • 相关推荐

php数组基于dom实现转换xml格式数据

  导语:下面小编要给大家提供的是php数组基于dom实现转换xml格式数据,大家可以参考阅读,更多详情请关注应届毕业生考试网。

  <?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();

  ?>

  运行结果如下:

  <?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数组基于dom实现转换xml格式数据】相关文章:

PHP 数组和字符串互相转换实现方法06-28

PHP如何使用DOM和simplexml读取xml文档07-22

php字符串与数组怎么转换10-04

PHP数据类型之数组变量详解10-04

PHP如何使用curl实现数据抓取09-27

php实现插入数组但不影响原有顺序的方法09-05

PHP数组函数知识10-24

如何实现PHP获取表单数据与HTML嵌入PHP脚本09-23

基于PHP+Ajax实现表单验证的详解08-22

PHP基于CURL进行POST数据上传的方法06-19