xml_parse_into_struct()函數是PHP中的內置函數,用於將XML數據解析為數組結構。 XML數據被解析為兩個並行的數組結構,第一個是包含指向值數組中值位置的指針的索引數組,第二個是包含來自解析的XML數據的值數組。
用法:
int xml_parse_into_struct( resource $xml_parser, string $data, array $values, array $index )
參數:該函數接受上述和以下所述的四個參數:
- $xml_parser:它是必填參數。它包含XML解析器的引用。
- $data:它是必填參數。它包含包含XML數據的字符串。
- $values:它是必填參數。它保存包含XML數據值的數組。
- $index:它是可選參數。它指定一個數組,該數組帶有指向$values中值位置的指針。
返回值:該函數成功返回1,失敗返回0。它與True和False不同。
注意:
- 此函數可用於PHP 4.0.0和更高版本。
- 這些示例可能不適用於在線IDE。因此,請嘗試在本地服務器或php托管服務器上運行它。
gfg.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<user>
<username> user123 </username>
<name> firstname lastname </name>
<phone> +91-9876543210 </phone>
<detail> I am John Doe. Live in Kolkata, India. </detail>
</user>
程序1:
<?php
// Create an xml parser
$xml_parser = xml_parser_create();
// Opening xml file in file stream
$filePointer = fopen("sample.xml", "r");
// Reading XML data from the
// specified XML file
$xml_data = fread($filePointer, 4096);
// Parseing XML data into an
// array structure
xml_parse_into_struct($xml_parser,
$xml_data, $values);
// Free to xml parse
xml_parser_free($xml_parser);
// Display array structured XML data
print_r($values);
// Closing the XML file
fclose($filePointer);
?>
輸出:
Array ( [0] => Array ( [tag] => USER [type] => open [level] => 1 [value] => ) [1] => Array ( [tag] => USERNAME [type] => complete [level] => 2 [value] => user123 ) [2] => Array ( [tag] => USER [value] => [type] => cdata [level] => 1 ) [3] => Array ( [tag] => NAME [type] => complete [level] => 2 [value] => firstname lastname ) [4] => Array ( [tag] => USER [value] => [type] => cdata [level] => 1 ) [5] => Array ( [tag] => PHONE [type] => complete [level] => 2 [value] => +91-9876543210 ) [6] => Array ( [tag] => USER [value] => [type] => cdata [level] => 1 ) [7] => Array ( [tag] => DETAIL [type] => complete [level] => 2 [value] => I am John Doe. Live in Kolkata, India. ) [8] => Array ( [tag] => USER [value] => [type] => cdata [level] => 1 ) [9] => Array ( [tag] => USER [type] => close [level] => 1 ) )
geeks.xml文件:
<?xml version="1.0"?>
<atoms>
<atom>
<name>Carbon</name>
<symbol>C</symbol>
<atomic_no>6</atomic_no>
</atom>
<atom>
<name>Hydrogen</name>
<symbol>H</symbol>
<atomic_no>1</atomic_no>
</atom>
<atom>
<name>Helium</name>
<symbol>He</symbol>
<atomic_no>2</atomic_no>
</atom>
<atom>
<name>Iron</name>
<symbol>Fe</symbol>
<atomic_no>26</atomic_no>
</atom>
</atoms>
程序2:
<?php
class Atom {
var $name; // Name of the element
var $symbol; // Symbol for the atom
var $atomic_no; // Atomic number
// Constructor for Atom class
function __construct( $aa ) {
// Initializing or setting the values
// to the field of the Atom class
foreach ($aa as $k=>$v)
$this->$k = $aa[$k];
}
}
function read_data($filename) {
// Read the XML database of atoms
$xml_data = implode("", file($filename));
// Creating an xml parser
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xml_parser, $xml_data, $values, $tags);
// Free to xml parser
xml_parser_free($xml_parser);
// Iterating through the structures
foreach ($tags as $key=>$val) {
if ($key == "atom") {
$atom_ranges = $val;
// Each contiguous pair of array entries
// are the lower and upper range for
// each molecule definition
for($i = 0; $i < count($atom_ranges); $i += 2) {
$offset = $atom_ranges[$i] + 1;
$len = $atom_ranges[$i + 1] - $offset;
// Parsing atom data
$tdb[] = parseAtoms(array_slice($values, $offset, $len));
}
}
else {
continue;
}
}
return $tdb;
}
// parseAtoms function to parse atom
function parseAtoms($mvalues) {
for ($i = 0; $i < count($mvalues); $i++) {
$ato[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new Atom($ato);
}
$db = read_data("atoms.xml");
echo "Database of atoms objects:\n";
print_r($db);
?>
輸出:
Database of atoms objects: Array ( [0] => Atom Object ( [name] => Carbon [symbol] => C [atomic_no] => 6 ) [1] => Atom Object ( [name] => Hydrogen [symbol] => H [atomic_no] => 1 ) [2] => Atom Object ( [name] => Helium [symbol] => He [atomic_no] => 2 ) [3] => Atom Object ( [name] => Iron [symbol] => Fe [atomic_no] => 26 ) )
參考: https://www.php.net/manual/en/function.xml-parse-into-struct.php
相關用法
- p5.js sq()用法及代碼示例
- d3.js d3.map.has()用法及代碼示例
- PHP next()用法及代碼示例
- p5.js day()用法及代碼示例
- p5.js pow()用法及代碼示例
- CSS var()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP pow( )用法及代碼示例
- PHP pi( )用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js str()用法及代碼示例
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | xml_parse_into_struct() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。