PHP Simple XML-Get

PHP Simple XML-Get


The Simple xml GET is used for getting the node values from an xml file.

Example

Following example shows how to get data from xml.

Hello.xml

Hello.xml is an xml file, it can be accessed by php file -

PHP SimpleXML - Get Node Values

Get the node values from the "Hello.xml" file:

The output of the code above will be:

 

PHP SimpleXML - Get Node Values of Specific Elements :

The following example gets the node value of the <title> element in the first and second <book> elements in the "books.xml" file: 

Example :

output :

PHP SimpleXML - Get Node Values - Loop :

The following example loops through all the <book> elements in the "books.xml" file, and gets the node values of the <title>, <author>, <year>, and <price> elements:

Output :

PHP SimpleXML - Get Attribute Values

The following example gets the attribute value of the "category" attribute of the first <book> element and the attribute value of the "lang" attribute of the <title> element in the second <book> element:

The output of the code above will be:

  COOKING
  en

 

PHP SimpleXML - Get Attribute Values - Loop

The following example gets the attribute values of the <title> elements in the "books.xml" file:

Example

The output of the code above will be:

  en
  en
  en-us
  en-us