当前位置: 移动技术网 > IT编程>开发语言>PHP > 用PHP实现读取和编写XML DOM代码

用PHP实现读取和编写XML DOM代码

2019年05月01日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

// 用 dom 读取 xml
$doc = new domdocument();
$doc->load(‘test.xml');
$books = $doc->getelementsbytagname(“book”);
foreach( $books as $book ){
$authors = $book->getelementsbytagname(“author”);
$author = $authors->item(0)->nodevalue; // nodevalue属 性可根据节点的类型来设置或返回某个节点的值。
$publishers = $book->getelementsbytagname(“publisher”);
$publisher = $publishers->item(0)->nodevalue;
$titles = $book->getelementsbytagname( ”title” );
$title = $titles->item(0)->nodevalue;
echo ”title: $title <br> author: $author <br> publisher: $publisher<br><hr><br>”;
}

/*
脚本首先创建一个 new domdocument 对象,用 load 方法把图书 xml 装入这个对象。之后,脚本 用 getelementsbyname 方法得到指定名称下的所有元素的列表。
在 book 节点的循环中,脚本用 getelementsbyname 方法获得 author、 publisher 和 title 标记的 nodevalue。nodevalue 是节点中的文本。脚本然后显示这些值。
*/
复制代码 代码如下:

// 用 sax 解析器读取 xml
$g_books = array();
$g_elem = null;
function startelement( $parser, $name, $attrs ){
global $g_books, $g_elem;
if ( $name == 'book' ) $g_books []= array();
$g_elem = $name;
}
function endelement( $parser, $name ){
global $g_elem;
$g_elem = null;
}
function textdata( $parser, $text ){
global $g_books, $g_elem;
if ( $g_elem == 'author' || $g_elem == 'publisher' || $g_elem == 'title' ){
$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
}
}
$parser = xml_parser_create();
xml_set_element_handler( $parser, ”startelement”, ”endelement” );
xml_set_character_data_handler( $parser, ”textdata” );
$f = fopen( 'test.xml', 'r' );
while( $data = fread( $f, 4096 ) ){
xml_parse( $parser, $data );
}
xml_parser_free( $parser );
foreach( $g_books as $book ){
echo $book['title'].” - ”.$book['author'].” - ”;
echo $book['publisher'].”\n”;
}

/*
脚本首先设置 g_books 数组,它在内存中容纳所有图书和图书信息,g_elem 变量保存脚本目前正在处理的标记的名称。然后脚 本定义回调函数。在这个示例中,回调函数是 startelement、endelement 和 textdata。在打开和关闭标记的时候,分别调 用 startelement 和 endelement 函数。在开始和结束标记之间的文本上面,调用 textdata。
在这个示例中,startelement 标记查找 book 标记,在 book 数组中开始一个新元素。然 后,textdata 函数查看当前元素,看它是不是 publisher、title 或 author 标记。如果是,函数就把当前文本放入当前图 书。
为了让解析继续,脚本用 xml_parser_create 函数创建解析器。然后,设置回调句柄。之后,脚本读取文件并把文件的大块 内容发送到解析器。在文件读取之后,xml_parser_free 函数删除解析器。脚本的末尾输出 g_books 数组的内容。
*/
// 用正则表达式解析 xml
复制代码 代码如下:

$xml = ”";
$f = fopen( 'test.xml', 'r' );
while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
fclose( $f );
preg_match_all( ”/\<book\>(.*?)\<\/book\>/s”, $xml, $bookblocks );
foreach( $bookblocks[1] as $block ){
preg_match_all( ”/\<author\>(.*?)\<\/author\>/”, $block, $author );
preg_match_all( ”/\<title\>(.*?)\<\/title\>/”, $block, $title );
preg_match_all( ”/\<publisher\>(.*?)\<\/publisher\>/”, $block, $publisher );
echo( $title[1][0].” - ”.$author[1][0].” - ”. $publisher[1][0].”\n” );
}

/*
我从不建议使用正则表达式读取 xml,但是有时它是兼容性最好的方式,因为正则表达式函数总是可用的。不要用正则表达式读取直接来自用户 的 xml,因为无法控制这类 xml 的格式或结构。应当一直用 dom 库或 sax 解析器读取来自用户的 xml。
*/
// 用 dom 编写 xml
复制代码 代码如下:

$books = array();
$books [] = array(
'title' => 'php hacks',
'author' => 'jack herrington',
'publisher' => ”o'reilly”
);
$books [] = array(
'title' => 'podcasting hacks',
'author' => 'jack herrington',
'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();

/*
在脚本的顶部,用一些示例图书装入了 books 数组。这个数据可以来自用户也可以来自数据库。
示例图书装入之后,脚本创建一个 new domdocument,并把根节点 books 添加到它。然后脚本为每本书 的 author、title 和 publisher 创建节点,并为每个节点添加文本节点。每个 book 节点的最后一步是重新把它添加到根节 点 books。
使用 dom 的真正价值在于它创建的 xml 总是格式正确的。但是如果不能用 dom 创建 xml 时该怎么办?
xml代码
复制代码 代码如下:

<?php
php 编写xml
$books = array();
$books [] = array(
'title' => 'php hacks',
'author' => 'jack herrington',
'publisher' => ”o'reilly”
);
$books [] = array(
'title' => 'podcasting hacks',
'author' => 'jack herrington',
'publisher' => ”o'reilly”
);
?>
<books>
<?php
foreach( $books as $book )
{
?>
<book>
<title><?php echo( $book['title'] ); ?></title>
<author><?php echo( $book['author'] ); ?>
</author>
<publisher><?php echo( $book['publisher'] ); ?>
</publisher>
</book>
<?php
}
?>
</books>

实例中用到的 test.xml 如下:
复制代码 代码如下:

<?xml version=”1.0″ encoding=”utf8″?>
<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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网