用PHP编写和读取XML的几种方式
时间:2023-11-18 22:30:27
一.使用DOM生成和读取XML文件
实例一:
<?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 <books>
$book = $books->appendChild($dom->createElement_x_x ('book'));
//add <title> element to <book>
$title = $book->appendChild($dom->createElement_x_x ('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
?>
实例二:
$aa = "111";
$xmlstr = <<<XML
<?xml version='1.0'?>
<document>
<title>{$aa}</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
$dom = new domDocument;
$dom->loadXML($xmlstr);
$test1 = $dom->saveXML();
$dom->save('test1.xml');
实例三:
test1.xml:
<?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>
example.php:
$doc = new DOMDocument();
$doc->load('test1.xml');
$books = $doc->getElementsByTagName("book");
foreach($books as $book){
$authors = $book->getElementsByTagName("author");
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
}
二.使用simple生成和读取xml文件
实例一:
<?
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<books>
<book>
<title>Great American Novel</title>
<characters>
<character>
<name>Cliff</name>
<desc>really great guy</desc>
</character>
<character>
<name>Lovely Woman</name>
<desc>matchless beauty</desc>
</character>
<character>
<name>Loyal Dog</name>
<desc>sleepy</desc>
</character>
</characters>
<plot>
Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark
at mailman.
</plot>
<success type='bestseller'>4</success>
<success type='bookclubs'>9</success>
</book>
</books>
XML;
//提取节点内容
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->book[0]->success as $success) {
switch((string) $success['type']) { // Get attributes as element indices
case 'bestseller':
echo $success. ' months on bestseller list<br>';
break;
case 'bookclubs':
echo $success. ' bookclub listings';
break;
}
}
//修改文本节点内容
$xml = new SimpleXMLElement($xmlstr);
$xml->book[0]->characters->character[0]->name = 'Big Cliff';
echo $xml->asXML();
//添加子元素的文本节点
$xml = new SimpleXMLElement($xmlstr);
$character = $xml->book[0]->characters->addChild('character');
$character->addChild('name', 'Yellow Cat');
$character->addChild('desc', 'aloof');
$success = $xml->book[0]->addChild('success', '2');
$success->addAttribute('type', 'reprints');
echo $xml->asXML();
?>
实例二:
if (file_exists('test1.xml')) { //读取xml文件
$xml = simplexml_load_file('test1.xml');
var_dump(xml);
} else {
exit('Failed to open test1.xml.');
}
三.DOM和simple互操作
DOM导入simpleXML:
<?php
$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);
$test2 = $dom->saveXML(); // put string in test2
$dom -> save('test2.xml'); // save as file
?>
simpleXML导入DOM:
<?php
$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
?>
标签:编写XML,读取XML
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
oracle中添加删除主键的方法
2023-06-30 00:28:52
如何在网页显示英语音标(附实例)
2010-01-12 17:07:00
![](https://img.aspxhome.com/file/UploadPic/20101/12/ipa_logo-93s.jpg)
浅析PHP中的字符串编码转换(自动识别原编码)
2023-09-08 08:04:44
python 执行shell命令并将结果保存的实例
2023-07-27 20:34:52
Python中常见的加密解密算法总结
2021-10-18 12:13:17
Python实现控制手机电脑拍照并自动发送邮箱
2022-10-08 01:52:33
![](https://img.aspxhome.com/file/2023/3/72133_0s.png)
解决SpringBoot启动过后不能访问jsp页面的问题(超详细)
2023-06-13 19:43:31
![](https://img.aspxhome.com/file/2023/9/64409_0s.jpg)
django美化后台django-suit的安装配置操作
2021-12-19 23:13:25
Python+OpenCV+pyQt5录制双目摄像头视频的实例
2021-12-22 02:07:24
![](https://img.aspxhome.com/file/2023/8/95718_0s.jpg)
Python实现简易的图书管理系统
2021-09-12 06:06:21
![](https://img.aspxhome.com/file/2023/0/107550_0s.jpg)
Python实现简单多线程任务队列
2022-07-29 13:21:43
IE6 iframe 横向滚动条问题
2009-01-18 13:31:00
Django forms表单 select下拉框的传值实例
2023-04-21 02:23:53
十六则Dreamweaver使用快技法
2009-07-05 18:55:00
javascript操作向表格中动态加载数据
2024-04-27 15:21:09
![](https://img.aspxhome.com/file/2023/6/135506_0s.jpg)
Golang使用第三方包viper读取yaml配置信息操作
2024-05-09 14:51:19
![](https://img.aspxhome.com/file/2023/1/127391_0s.jpg)
Pytest断言的具体使用
2023-12-15 06:30:04
MySQL数据库的自动备份与数据库被破坏后的恢复
2010-03-18 15:30:00
Python实现批量转换文件编码的方法
2023-06-02 20:03:49
python输入整条数据分割存入数组的方法
2022-04-10 06:10:25