xml_parse_into_struct
时间:2008-05-04 来源:小飞虫
(PHP 4, PHP 5)
xml_parse_into_struct — 将 XML 数据解析到数组中
说明int xml_parse_into_struct ( resource $parser, string $data, array &$values [, array &$index] )
该函数将 XML 文件解析到两个对应的数组中,index 参数含有指向 values 数组中对应值的指针。最后两个数组参数可由指针传递给函数。
注意: xml_parse_into_struct() 失败返回 0,成功返回 1。这和 FALSE 与 TRUE 不同,使用例如 == 的运算符时要注意。
范例1:显示了由该函数生成的数组的内部结构。我们简单地将一个 note 嵌入到一个 para 标记中,解析后我们可以打印出生成的数组的结构:
例 2423. xml_parse_into_struct() 示例
= "simple note";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
?>
运行以上代码,我们得到的输出将是:
Index array
Array
(
[PARA] => Array
(
[0] => 0
[1] => 2
)
[NOTE] => Array
(
[0] => 1
)
)
Vals array
Array
(
[0] => Array
(
[tag] => PARA
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => NOTE
[type] => complete
[level] => 2
[value] => simple note
)
[2] => Array
(
[tag] => PARA
[type] => close
[level] => 1
)
)
范例二:如果您的 XML 文档很复杂,基于该文档的事件处理(Event-driven)解析(基于 expat 扩展库)也会对应的变得复杂。该函数生成的并非 DOM 风格的对象,而是横向的树状结构。因此,我们能够方便的建立表达 XML 文件数据的对象。我们假设以下 XML 文件表示一个关于氨基酸信息的小型数据库:
例 2424. moldb.xml - 分子信息的小型数据库
?xml version="1.0" ?>
moldb>
molecule>
name>Alaninename>
symbol>alasymbol>
code>Acode>
type>hydrophobictype>
molecule>
molecule>
name>Lysinename>
symbol>lyssymbol>
code>Kcode>
type>chargedtype>
molecule>
moldb>
以下是解析该文档并生成相应对象的代码:
例 2425. parsemoldb.php - 将 moldb.xml 解析到分子(molecular)对象的数组中
class AminoAcid {
var $name; // aa 姓名
var $symbol; // 三字母符号
var $code; // 单字母代码
var $type; // hydrophobic, charged 或 neutral
function AminoAcid ($aa)
{
foreach ($aa as $k=>$v)
$this->$k = $aa[$k];
}
}
function readDatabase($filename)
{
// 读取 aminoacids 的 XML 数据
$data = implode("",file($filename));
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $tags);
xml_parser_free($parser);
// 遍历 XML 结构
foreach ($tags as $key=>$val) {
if ($key == "molecule") {
$molranges = $val;
// each contiguous pair of array entries are the
// lower and upper range for each molecule definition
for ($i=0; $i count($molranges); $i+=2) {
$offset = $molranges[$i] + 1;
$len = $molranges[$i + 1] - $offset;
$tdb[] = parseMol(array_slice($values, $offset, $len));
}
} else {
continue;
}
}
return $tdb;
}
function parseMol($mvalues)
{
for ($i=0; $i count($mvalues); $i++) {
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new AminoAcid($mol);
}
$db = readDatabase("moldb.xml");
echo "** Database of AminoAcid objects:\n";
print_r($db);
?>
在执行完 parsemoldb.php 后,变量 $db 将包含有一个由 AminoAcid 对象组成的数组,该脚本的输出如下:
** Database of AminoAcid objects:
Array
(
[0] => aminoacid Object
(
[name] => Alanine
[symbol] => ala
[code] => A
[type] => hydrophobic
)
[1] => aminoacid Object
(
[name] => Lysine
[symbol] => lys
[code] => K
[type] => charged
)
)
范例三:Alf Marius Foss Olsen
/*
An easy lightweight (Array ->) XML -> Array algorithm..
Typical case: You have an array you want to export to an external server,
so you make XML out of it, exports it, and "on the other side"
make it into an array again. These two functions will take care
of that last part, ie XML -> Array
NOTE! The function XMLToArray assumes that the XML _dont_ have nodes on the
same level with the same name, then it just wont work. This is not a
problem, as this case deals with Array -> XML -> Array, and an array
cant have to identical indexes/keys.
*/
function XMLToArray($xml) {
$parser = xml_parser_create('ISO-8859-1'); // For Latin-1 charset
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); // Dont mess with my cAsE sEtTings
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); // Dont bother with empty info
xml_parse_into_struct($parser, $xml, $values);
xml_parser_free($parser);
$return = array(); // The returned array
$stack = array(); // tmp array used for stacking
foreach($values as $val) {
if($val['type'] == "open") {
array_push($stack, $val['tag']);
} elseif($val['type'] == "close") {
array_pop($stack);
} elseif($val['type'] == "complete") {
array_push($stack, $val['tag']);
setArrayValue($return, $stack, $val['value']);
array_pop($stack);
}//if-elseif
}//foreach
return $return;
}//function XMLToArray
function setArrayValue(&$array, $stack, $value) {
if ($stack) {
$key = array_shift($stack);
setArrayValue($array[$key], $stack, $value);
return $array;
} else {
$array = $value;
}//if-else
}//function setArrayValue
// USAGE:
$xml =
Some text
Some text
\n
QQQ;
$array = XMLToArray($xml);
print "";
print_r($array);
print "";
// Output:
//
// Array
// (
// [root] => Array
// (
// [node1] => Some text
// [node2a] => Array
// (
// [node2b] => Array
// (
// [node2c] => Some text
// )
// )
// )
// )
?>
范例4:vinod at citadel-soft dot com
My previous code was having some bugs in, that is fixed now
class CSLXmlReader {
private $tagstack;
private $xmlvals;
private $xmlvarArrPos;
private $xmlfile;
function __construct($filename) // constructor to intialize the stack and val array
{
$this->tagstack = array(); // contain the open tags till now
$this->xmlvals = array();
$this->xmlvarArrPos = $this->xmlvals; // temporary variable to hold the current tag position
$this->xmlfile = $filename;
}
function readDatabase()
{
// read the XML database
$data = implode("", file($this->xmlfile));
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $tags);
xml_parser_free($parser);
foreach($values as $key => $val) //
{
if($val['type'] == "open")
{
array_push($this->tagstack, $val['tag']);
$this->getArrayPath();
if(count($this->xmlvarArrPos) > 0 && (!array_key_exists(0,$this->xmlvarArrPos)))
{
$temp1 = $this->xmlvarArrPos;
$this->xmlvarArrPos = array();
$this->xmlvarArrPos[0] = $temp1;
array_push($this->tagstack, 1);
}
else if( array_key_exists(0,$this->xmlvarArrPos)){
$opncount = count($this->xmlvarArrPos);
array_push($this->tagstack, $opncount);
}
$tagStackPointer += 1;
}else if($val['type'] == "close")
{
while( $val['tag'] != ($lastOpened = array_pop($this->tagstack))){}
}else if($val['type'] == "complete")
{
$this->getArrayPath();
if( array_key_exists($val['tag'],$this->xmlvarArrPos))
{
if(array_key_exists(0,$this->xmlvarArrPos[$val['tag']]))
{
$elementCount = count($this->xmlvarArrPos[$val['tag']]);
$this->xmlvarArrPos[$val['tag']][$elementCount] = $val['value'];
}else
{
$temp1 = $this->xmlvarArrPos[$val['tag']];
$this->xmlvarArrPos[$val['tag']] = array();
$this->xmlvarArrPos[$val['tag']][0] = $temp1;
$this->xmlvarArrPos[$val['tag']][1] = $val['value'];
}
} else
{
$this->xmlvarArrPos[$val['tag']] = $val['value'];
}
}
}
reset($this->xmlvals);
return $this->xmlvals;
}
function getArrayPath()
{
reset($this->xmlvals);
$this->xmlvarArrPos = &$this->xmlvals;
foreach($this->tagstack as $key)
{
$this->xmlvarArrPos = &$this->xmlvarArrPos[$key];
}
}
}
$readerObj = new CSLXmlReader("test.xml");
$xmlvals = $readerObj->readDatabase();
echo "########## XML Values In array as the multidimentional array #############\n";
echo "";
print_r($xmlvals);
echo "";
?>
范例5:XML to Array and backwards
Here the XML with PHP solution: XML->Array and Array->XML.
Work with it as with usual array.
Sources are here:
http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html
(leave me comments:)
Example #1 (1.xml):
333
555
777
sdf rr
The code:
$xml=xml2ary(file_get_contents('1.xml'));
print_r($xml);
Here is the Array result:
Array
(
[ddd] => Array (
[_c] => Array (
[_p] => Array *RECURSION*
[onemore] => Array (
[_a] => Array (
[dd] => 55
)
[_c] => Array (
[_p] => Array *RECURSION*
[tt] => Array (
[0] => Array (
[_v] => 333
)
[1] => Array (
[_a] => Array (
[ss] => s1
)
[_v] => 555
)
[2] => Array (
[_v] => 777
)
)
)
)
[two] => Array (
[_v] => sdf rr
)
)
)
)
范例6:That parser also has a problem in which it will not parse more items than the current level it is on. That is, parsing this: ABC
Will only result in this: A B
C is never processed
It might be better with something like this:
$file = get_url('
http://news.search.yahoo.com/news/rss?p=current+events
', URL_CONTENT);
$data = $file['content'];
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
//Uncomment the lines below to see the entire structure of your XML document
//echo "INDEX: \n";
//print_r ($index);
//echo "\n \n \n VALUES:";
//print_r ($vals);
//echo "";
$params = array();
$level = array();
$start_level = 1;
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem)) {
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
} else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$php_stmt = '$params';
while($start_level ";
print_r ($params);
echo "";
范例7:This is a quick fix for parsing XML from remote URL, some of the example above will work when trying to parse on your local server without "
http://
" but not when trying to parse from remote server using "
http://www.URL
"...
="
http://www.URL.com/file.XML
";
$xml_parser = xml_parser_create();
$handle = fopen($file, "rb");
$contents = '';
while (!feof($handle)) {
$data .= fread($handle, 8192);
}
fclose($handle);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem)) {
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
} else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
echo "";
print_r ($params);
echo "";
?>
范例8:一个好用的解析函数
function mc_parse_xml($filename)
{
$xml = file_get_contents($filename);
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $index);
xml_parser_free($p);
for ($i=0;$icount($values);$i++) {
if (isset($values[$i]['attributes'])) {
$parent = $values[$i]['tag'];
$keys = array_keys($values[$i]['attributes']);
for ($z=0;$zcount($keys);$z++)
{
$content[$parent][$i][$keys[$z]] = $values[$i]['attributes'][$keys[$z]];
if (isset($content[$parent][$i]['VALUE'])) $content[$parent][$i]['VALUE'] = $values[$i]['value'];
}
}
}
foreach ($content as $key => $values) {
$content[$key] = array_values($content[$key]);
}
if (is_array($content)) return $content;
else return false;
}
?>
范例9:一个简单的解析函数
class Xml
{
var $tag;
var $value;
var $attributes;
var $next;
}
function xml2array($xml_string)
{
$Parser = xml_parser_create();
xml_parser_set_option($Parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($Parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($Parser, $xml_string, $Xml_Values);
xml_parser_free($Parser);
$XmlClass = array();
$LastObj = array();
$NowObj = &$XmlClass;
foreach($Xml_Values as $Xml_Key => $Xml_Value)
{
$Index = count($NowObj);
if($Xml_Value["type"] == "complete")
{
$NowObj[$Index] = new Xml;
$NowObj[$Index]->tag = $Xml_Value["tag"];
$NowObj[$Index]->value = $Xml_Value["value"];
$NowObj[$Index]->attributes = $Xml_Value["attributes"];
}
elseif($Xml_Value["type"] == "open")
{
$NowObj[$Index] = new Xml;
$NowObj[$Index]->tag = $Xml_Value["tag"];
$NowObj[$Index]->value = $Xml_Value["value"];
$NowObj[$Index]->attributes = $Xml_Value["attributes"];
$NowObj[$Index]->next = array();
$LastObj[count($LastObj)] = &$NowObj;
$NowObj = &$NowObj[$Index]->next;
}
elseif($Xml_Value["type"] == "close")
{
$NowObj = &$LastObj[count($LastObj) - 1];
unset($LastObj[count($LastObj) - 1]);
}
}
return $XmlClass;
}
$String = "
MyParser
Name 1
A note 1
";
$Xml = xml2array($String);
print_r($Xml);
?>
This exemple will show :
Array
(
[0] => Xml Object
(
[tag] => parser
[value] =>
[attributes] =>
[next] => Array
(
[0] => Xml Object
(
[tag] => parseur_name
[value] => MyParser
[attributes] =>
[next] =>
)
[1] => Xml Object
(
[tag] => category
[value] =>
[attributes] =>
[next] => Array
(
[0] => Xml Object
(
[tag] => name
[value] => Name 1
[attributes] =>
[next] =>
)
[1] => Xml Object
(
[tag] => note
[value] => A note 1
[attributes] =>
[next] =>
)
)
)
)
)
)
相关阅读 更多 +
排行榜 更多 +