代码如下 | 复制代码 |
include 'mysql.php';
$xml->openUri('hello.xml'); // or 'php://output' $xml->setIndentString(' '); // <rss version="2.0"> // for item $xml->endDocument(); |
openUri('') 方法的参数可以是一个文件,那么xml数据就写入到这个文件
或者 php://output 输出到缓冲区,然后 flush方法输出到页面
代码如下 | 复制代码 |
<?php |
正则解析
代码如下 | 复制代码 |
<?php $xml = ""; $f = fopen( 'books.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" ); } ?> |
books.xml文件如下
代码如下 | 复制代码 |
<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> |
下面就给大家举一个小小的例子用parser函数来读取xml数据:
代码如下 | 复制代码 |
<?php
fclose($filehandler);
function characterData($parser_instance, $xml_data) //读取数据时的函数 function endElement($parser_instance, $element_name) //结束标签事件的函数 ?> |
xml文件代码如下:
代码如下 | 复制代码 |
<?xml version="1.0"?> |
parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签、 读取数据、结束标签。
也就是说在对xml进行处理的时候每当遇到起始标签、数据和结束标签的时候函数会做相应的动作来完成对xml数据的转换。
与大多数流行的 Web 服务如 twitter 通过开放 API 来提供数据一样,它总是能够知道如何解析 API 数据的各种传送格式,包括 JSON,XML 等等。
$json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php教程"]} ';
$obj=json_decode($json_string);
echo $obj->name; //prints foo
echo $obj->interest[1]; //prints php
PHP解析XML 数据
$xml_string="<?xml version='1.0'?>
<users>
<user id='398'>
<name>Foo</name>
<email>foo@bar.com</name>
</user>
<user id='867'>
<name>Foobar</name>
<email>foobar@foo.com</name>
</user>
</users>";
//load the xml string using simplexml
$xml = simplexml_load_string($xml_string);
//loop through the each node of user
foreach ($xml->user as $user)
{
//access attribute
echo $user['id'], ' ';
//subnodes are accessed by -> operator
echo $user->name, ' ';
echo $user->email, '<br />';
}
<?
$elementLevel = 0 ;
function array_Xml($array, $keys = '')
{
global $elementLevel;
if(!is_array($array))
{
if($keys == ''){
return $array;
}else{
return "n<$keys>" . $array . "</$keys>";
}
}else{
foreach ($array as $key => $value)
{
$haveTag = true;
if (is_numeric($key))
{
$key = $keys;
$haveTag = false;
}
/**
* The first element
*/
if($elementLevel == 0 )
{
$startElement = "<$key>";
$endElement = "</$key>";
}
$text .= $startElement."n";
/**
* Other elements
*/
if(!$haveTag)
{
$elementLevel++;
$text .= "<$key>" . array_Xml($value, $key). "</$key>n";
}else{
$elementLevel++;
$text .= array_Xml($value, $key);
}
$text .= $endElement."n";
}
}
return $text;
}
?>
class ArrayToXML
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops教程 through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}
if ($xml == null)
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
}
// loop through the data passed in.
foreach($data as $key => $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = "unknownNode_". (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
$node = $xml->addChild($key);
// recrusive call.
ArrayToXML::toXml($value, $rootNodeName, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
}
function arrtoxml($arr,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0");
}
if(!$item){
$item = $dom->createElement("root");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
$itemx = $dom->createElement(is_string($key)?$key:"item");
$item->appendChild($itemx);
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}else {
arrtoxml($val,$dom,$itemx);
}
}
return $dom->saveXML();
}
XML 函数允许我们解析 XML 文档,但无法对其进行验证。
XML 是一种用于标准结构化文档交换的数据格式。您可以在我们的 XML 教程 中找到更多有关 XML 的信息。
该扩展使用 Expat XML 解析器。
<?php教程
function walk_tree ($node, $depth = 0) {
for ($i = 0, $indent = ''; $i < $depth; $i++)
$indent .= ' ';
if ($node->type == XML_ELEMENT_NODE) {
print ($indent . $node->tagname . "n");
$kids = $node->children ();
$nkids = count ($kids);
if ($nkids > 0) {
$depth++;
for ($i = 0; $i < $nkids; $i++)
walk_tree ($kids[$i], $depth);
$depth--;
}
}
}
$doc = xmldocfile ('contact.xml');
print ("<pre>n");
walk_tree ($doc->root ());
print ("</pre>n");
?><!--
<contact id="43956">
<personal>
<name>
<first>J</first>
<middle>J</middle>
<last>J</last>
</name>
<title>Manager</title>
<employer>National</employer>
<dob>1971-12-22</dob>
</personal>
</contact>
-->