Change language

PHP SimpleXMLElement XPath () Function

Function SimpleXMLElement::xpath() - it is a built-in function in PHP that performs an XPath query on an XML document.Syntax:
SimpleXMLElement::xpath ($path) 
Parameters:This function takes one required parameter, $path . It is used to specify the XPath path of the XML document.Return Value:Returns a SimpleXMLElements array on success, or False on failure.Note .This feature is available in PHP 5.2.0 and later.Example :  
// Load XML document into $user $user = < < < XML < user > 12345 < username > Geeks123 < / username > < name > GeeksforGeeks < / name > + 91-XXXXXXXXXX < / phone > < detail font-color = "blue" font-size = "24px" > Noida India < / detail > < / user >
XML;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Get xpaths $result = $xml -> xpath ( "username" );  
// Printout print_r ( $result );  
?>
Exit:
Array ([ 0] = > SimpleXMLElement Object ([0] = > Geeks123)) 
Example 2:  
// Load XML document into $user $user = < < < XML < user xmlns: user_id = " http://engineerforengineer.org/user " > < single_user id = "1" > < user_id: id > 12345 < / user_id: id > < username > Geeks123 < / username > < name > GeeksforGeeks < / name > + 91-XXXXXXXXXX < / phone > < detail font-color = "blue" font-size = "24px" > Noida India < / detail > < / single_user > < single_user id = "2" > < user_id: id > 15980 < / user_id: id > < username > Geeks54321 < / username > < name > Geeks < / name > + 91-XXXXXXXXXX < / phone > < detail font-color = "blue" font-size = "24px" > Noida India < / detail > < / single_user >
< / user >
XML;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Register xpath namespace $xml -> registerXPathNamespace ( ’u’ , http: //engineerforengineer.org/user );  
// Get xpaths $result = $xml -> xpath ( ’// u: id’ );  
// Printout foreach ( $result as $id ) { echo $id . "
"
; }  
?>
Exit:
12345 
15980
Example 3:  
// Load XML document into $user $user = < < < XML < user xmlns: user_id = " http: // engineerforengineer.org/user " > < single_user id = " 1 " xmlns: name = " http://engineerforengineer.org/user/name " > < user_id: id > 12345 < / user_id: id > < username > rakesh123 < / username > < name: firstname > Rakesh < / name: firstname > < name: lastname > Kumar < / name: lastname > + 91-XXXXXXXXXX < / phone > < detail > Noida India < / detail > < / single_user > < single_user id = " 2 " xmlns: name = " http://engineerforengineer.org/user/name " > < user_id: id > 57833 < / user_id: id > < username > man123 < / username > < name: firstname > Manjeet < / name: firstname > < name: lastname > Singh < / name: lastname > + 91-XXXXXXXXXX < / phone > < detail > Kolkata, India < / detail > < / single_user > < single_user id = " 3 " xmlns: name = " http://engineerforengineer.org/user/name " > < user_id: id > 98944 < / user_id: id > < username > ak98 < / username > < name: firstname > Ak < / name: firstname > < name: lastname > Singh < / name: lastname > + 91-XXXXXXXXXX < / phone > < detail > Noida India < / detail > < / single_user > < / user >
XML;  
// Load the string as a simple xml object $xml = simplexml_load_string ( $user );  
// Register xpath namespace $xml -> registerXPathNamespace ( ’u’ , http: //engineerforengineer.org/user ); $xml -> registerXPathNamespace ( ’un’ , http://engineerforengineer.org/user/name );  
// Get xpaths $result = $xml -> xpath ( ’// u: id’ ); $result_f_name = $xml -> xpath ( ’// un: firstname’ ); $result_l_name = $xml -> xpath ( ’// un: lastname’ );  
// Printout foreach ( $result as $id ) { echo $id . "
"
; } foreach ( $result_f_name as $f_name ) {   echo $f_name . "
"
; } foreach ( $result_l_name as $l_name ) {   echo $l_name . "
"
; }  
?>
Exit:
12345 
57833
98944
Rakesh, Manjeet, Ak, Kumar, Singh, Singh
Link: https://www.php.net/manual/en/simplexmlelement.xpath.php

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

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