欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用PHP编写和读取XML的几种方式

程序员文章站 2023-11-19 13:08:04
一.使用dom生成和读取xml文件 实例一: 复制代码 代码如下:
一.使用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
?>