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

前端之家收集整理的这篇文章主要介绍了php基于dom实现的图书xml格式数据示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

PHP;"> 'PHP Hacks','author' => 'Jack Herrington','publisher' => "O'Reilly" ); $books [] = array( 'title' => 'Podcasting Hacks','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(); ?>

运行结果如下:

Jack Herrington PHP Hacks O'Reilly Jack Herrington Podcasting Hacks O'Reilly

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:

在线格式化XML/在线压缩XML:

XML在线压缩/格式化工具:

XML代码在线格式化美化工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://www.f2er.com/php/18060.html

猜你在找的PHP相关文章