`
aaron_ch
  • 浏览: 172524 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

PHP and XML

    博客分类:
  • PHP
阅读更多

Use DOM
&lt?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
?&gt

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');
?&gt

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
?&gt

分享到:
评论

相关推荐

    PHP XML and Web Services

    PHP xml web services

    PHP and MYSQL Bilbe [英文原版]

    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...

    整合PHP和XML(CHM)

    学习如何使用SAX,XSLT,和XPath来操作XML文档,及使用XML-RPC 协议来访问远程计算机的过程等,英文,原名称为Integrating PHP and XML

    XML Processing with Perl, Python, and PHP (2002).pdf

    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 ...

    PHP Oracle Web Development: Data Processing, Security, Caching, XML, Web Services, and Ajax

    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

    Pro.PHP.XML.and.Web.Services.2006

    Beginning XML with DOM and Ajax From Novice to Professional .pdf

    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 ...

    XML and PHP

    帮助那些不熟悉使用PHP 处理XML 的新手使用PHP 环境中的DOM 和SimpleXML 读取、解析、操作并编写简单短小的XML 文件。

    php5 and xml示例

    http://trash.chregu.tv/phpconf2003/examples/ PHP5的XML新特性 作者 Christian Stocker 翻译 ice_berg16(寻梦的稻草人) 面向的读者 这篇文章的面向对象是所有对PHP5的XML新功能感兴趣的各个水平的PHP开发者。...

    整合PHP和XML

    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 [Chromecast Linux XML PHP Python]

    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 Express-C The Developer Handbook for XML PHP CC++ Java and dotNET

    DB2 开发,DB2 Express-C The Developer Handbook for XML PHP CC++ Java and dotNET

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    XML越来越热,关于XML的基础教程网络上也随处可见。可是一大堆的概念和术语往往让人望而生畏,很多朋友问我:XML到底有什么用,我们是否需要学习它?我想就我个人学习过程的心得和经验,写一篇比较全面的介绍文章。...

    php+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

    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.1484206061

    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

    xml浅学笔记

    AJAX: Asynchronous Javascript And Xml 1、什么是XML eXtensible Markup Language 可扩展的 标记 语言 XML的标记没有预定义过,需要自行定义 XML的宗旨是做数据传递的,而非数据显示 2、XML的语法规范 XML...

    xml-rpc 1.5.1

    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&apos;t have PHP&apos;s mbstring extension ...

    AJAX and PHP.pdf

    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编写和读取XML的几种方式

    php //Creates XML string and XML document using the DOM $dom = new DomDocument(‘1.0’); //add root – &lt;books&gt; $books = $dom-&gt;appendChild($dom-&gt;createElement_x_x (‘books’)); //add &lt;book&gt; element to ...

Global site tag (gtag.js) - Google Analytics