Change language

PHP Xml_parse_into_struct () function

|
Syntax:
int xml_parse_into_struct (resource $xml_parser, string $data , array $values, array $index)
Parameters:this function takes four parameters as above and described below:
  • $xml_parser:required. It contains a link to the XML parser.
  • $data:This is a required parameter. It contains a string containing XML data.
  • $values:This is a required parameter. It contains an array containing XML data values.
  • $index:This is an optional parameter. It defines an array with pointers to the location of the values ​​in $values.
Return Value:This function returns 1 on success and 0 on error. This is not the same as True and False.Notes:
  • This function is available for PHP 4.0.0 and newer.
  • These examples may not work in the online IDE. So, try running it on your local server or on the php hosts.
gfg.xml file: 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 > Program 1:  
// Create xml parser $xml_parser = xml_parser_create();  
// Opening an xml file in a file stream $filePointer = fopen ( " sample.xml " , " r " );  
// Read XML data from
// specified XML file $xml_data = fread ( $filePointer , 4096);  
// Parse XML data into // array structure xml_parse_into_struct ( $xml_parser , $xml_data , $values ​​ );  
// Free xml analysis xml_parser_free ( $xml_parser );  
// Show XML structured array data print_r ( $values ​​ );  
// Close the XML file fclose ( $filePointer );  
?>
Output:
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] = > U SER [type] = > close [level] = > 1))
engineer.xml file: 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 > Program 2: class Atom { var $name ; // Element name var $symbol ; // Symbol for the atom var $atomic_no ; // Atomic number // Constructor for the Atom class function __ construct ( $aa ) { // Initialize or set values ​​ // in the Atom class field

foreach ( $aa as $k = > $v )   $this -> $k = $aa [ $k ]; } } function read_data ( $filename ) { // Read the XML database of atoms $xml_data = implode ( "" , file ( $filename )) ; // Create 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 xml parser xml_parser_free ( $xml_parser ); // Iterate over structures foreach ( $tags as $key = > $val ) { if ( $key == " atom " ) { $atom_ranges = $val ; // Each continuous pair of array elements // lower and upper range // definition of each molecule for ( $i = 0; $i < count ( $atom_ranges ); $i + = 2) { $offset = $atom_ranges [ $i ] + 1; $len = $atom_ranges [ $i + 1] - $offset ; // Analyze atom data $tdb [] = parseAtoms ( array_slice ( $values ​​ , $offset , $len )); } } else { continue ; } } return $tdb ; }  
// parseAtoms function to parse the 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:" ; print_r ( $db );  
?>
Output:
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))
Link: https://www.php.net/manual/ en / function.xml-parse-into-struct.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method