Use DOM
<?php
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <books>
$books = $dom->appendChild($dom->createElement('books'));
//add <book> element to <books>
$book = $books->appendChild($dom->createElement('book'));
//add <title> element to <book>
$title = $book->appendChild($dom->createElement('title'));
//add <title> text node element to <title>
$title->appendChild($dom->createTextNode('Great American
Novel'));
//generate xml
$dom->formatOutput = true; // set the formatOutput attribute of
domDocument to true
// save XML as string or file
$test1 = $dom->saveXML(); // put string in test1
$dom -> save('test1.xml'); // save as file
?>
Use SimpleXML
$sxe = simplexml_load_string('<books><book><title>Great American
Novel</title></book></books>');
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
echo $dom->saveXML('test2.xml');
?>
DOM SimpleXML
$dom = new domDocument;
$dom->loadXML('<books><book><title>Great American
Novel</title></book></books>');
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title; // Great American Novel
?>
分享到:
相关推荐
PHP xml web services
Chapter 40: PHP and XML 731 Chapter 41: Web Services 757 Chapter 42: Graphics 775 Part V: Case Studies 799 Chapter 43: Weblogs 801 Chapter 44: User Authentication 819 Chapter 45: A User-Rating System...
学习如何使用SAX,XSLT,和XPath来操作XML文档,及使用XML-RPC 协议来访问远程计算机的过程等,英文,原名称为Integrating PHP and XML
Alternatively, I can store everything in XML and access, process, and update the infor- mation directly. If I want, I can even query the XML document using XQL. I can use it to exchange information ...
build Ajax-driven PHP/Oracle solutions. Building and deploying PHP applications on Oracle Database combines the power and robustness of Oracle with the easy, rapid development of open-source PHP to ...
Pro.PHP.XML.and.Web.Services.2006
It covers two server- side languages: PHP 5 and .NET 2.0. The last chapters of the book deconstruct two XML applications: a News application and a Community Weather Portal application. The book ...
帮助那些不熟悉使用PHP 处理XML 的新手使用PHP 环境中的DOM 和SimpleXML 读取、解析、操作并编写简单短小的XML 文件。
http://trash.chregu.tv/phpconf2003/examples/ PHP5的XML新特性 作者 Christian Stocker 翻译 ice_berg16(寻梦的稻草人) 面向的读者 这篇文章的面向对象是所有对PHP5的XML新功能感兴趣的各个水平的PHP开发者。...
Learn how to use SAX, XSLT, and XPath to manipulate XML documents, as well as use of XML-RPC protocol for accessing procedures on a remote computer, and much more
Technical 5 in 1 Box Set: Book 1: Chromecast + Book 2: Linux for Beginners + Book 3: XML Programming + Book 4: PHP Programming + Book 5: Python Programming by Jack Brandson English | 30 Jul 2016 | ...
DB2 开发,DB2 Express-C The Developer Handbook for XML PHP CC++ Java and dotNET
XML越来越热,关于XML的基础教程网络上也随处可见。可是一大堆的概念和术语往往让人望而生畏,很多朋友问我:XML到底有什么用,我们是否需要学习它?我想就我个人学习过程的心得和经验,写一篇比较全面的介绍文章。...
Use this forum freely, and at your own risk. Please link to one of my sites if you use this source: - http://outer-court.com - http://questml.com - http://netpadd.com - http://opentrivia.com -...
jQGrid 3.5 jQuery UI 1.7.2 Lightness theme (get more here) Sample static JSON file Sample static XML file (do not put this ...Complete JSON.php and XML.php parser mySQL script for rapid demo integration
PHP and MySQL Recipes: A Problem-Solution Approach supplies you with complete code for all of ...Chapter 14: XML, RSS, WDDX, and SOAP Chapter 15: Data Exchange with JSON Chapter 16: Using MySQL Databases
AJAX: Asynchronous Javascript And Xml 1、什么是XML eXtensible Markup Language 可扩展的 标记 语言 XML的标记没有预定义过,需要自行定义 XML的宗旨是做数据传递的,而非数据显示 2、XML的语法规范 XML...
The automatic base64 encoding can be turned on via the new XML_RPC_Client::setAutoBase64() method. The auto-encoding is a workaround for systems that don't have PHP's mbstring extension ...
This chapter shows you how to use XML and JSON with PHP (so that you can create server-side code that communicates with your JavaScript client), how to implement error-handling code in your server-...
php //Creates XML string and XML document using the DOM $dom = new DomDocument(‘1.0’); //add root – <books> $books = $dom->appendChild($dom->createElement_x_x (‘books’)); //add <book> element to ...