📜  PHP | xml_parse_into_struct()函数

📅  最后修改于: 2022-05-13 01:56:33.889000             🧑  作者: Mango

PHP | xml_parse_into_struct()函数

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。它与真假不同。

笔记:

  • 此函数适用于PHP 4.0.0 及更新版本。
  • 这些示例可能不适用于在线 IDE。因此,尝试在本地服务器或PHP托管服务器上运行它。

gfg.xml 文件:

XML


     user123 
     firstname lastname 
     +91-9876543210 
     I am John Doe. Live in Kolkata, India. 


PHP


XML


    
        Carbon
        C
        6
    
    
        Hydrogen
        H
        1
    
    
        Helium
        He
        2
    
    
        Iron
        Fe
        26
    


PHP
$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);
 
?>


方案一:

PHP


输出:

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
        )
)

极客.xml 文件:

XML



    
        Carbon
        C
        6
    
    
        Hydrogen
        H
        1
    
    
        Helium
        He
        2
    
    
        Iron
        Fe
        26
    

方案二:

PHP

$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/函数.xml-parse-into-struct。 PHP