WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP SimpleXML - Get Node/Attribute Values


SimpleXML is a PHP expansion that enables us to effectively control and get XML data.


In this instructional exercise you will investigation of Node attribute

PHP SimpleXML - Get Node Values

Get the hub esteems from the " " file:

Example

<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot make object");
echo $xml->to . "<br>";
reverberation $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>

The yield of the code above will be:

Tove
Jani
Reminder
Remember me this end of the week!

Another XML File

Assume we have a XML record called "books.xml", that resembles this: 

<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Jhone</author>
    <year>2017</year>
    <price>30.00</price>
  </book>
  <book category="WEB">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2017</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2008</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2008</year>
    <price>39.95</price>
  </book>
</bookstore>

PHP SimpleXML - Get Node Values of Specific Elements

The following precedent gets the hub estimation of the <title> component in the first what's more, second <book> components in the "books.xml" file: 

Example

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot make object");
echo $xml->book[0]->title . "<br>";
echo $xml->book[1]->title;
?>

The yield of the code above will be:

PHP and MySQL Web Development
Head First PHP and MySQL: A Brain-Friendly Guide

PHP SimpleXML - Get Node Values - Loop

The finishing precedent circles all the <book> components in the "books.xml" document, what's more, gets the hub estimations of the <title>, <author>, <year>, and <price> elements:

Example

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot make object");
foreach($xml->WEB() as $books) {
    reverberation $books->title . ", ";
    reverberation $books->author . ", ";
    reverberation $books->year . ", ";
    reverberation $books->price . "<br>";
}
?>

The yield of the code above will be:

PHP and MySQL Web Development, Luke Welling, 2017, 30.00
Head First PHP and MySQL: A Brain-Friendly Guide, J K. Rowling, 2017, 29.99
XQuery Kick Start, James McGovern, 2008, 49.99
Learning XML, Erik T. Beam, 2008, 39.95

PHP SimpleXML - Get Attribute Values

The following precedent gets the property estimation of the "category" quality of the first <book> component and the characteristic estimation of the "lang" property of the <title> component in the second <book> element:

Example

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot make object");
echo $xml->book[0]['category'] . "<br>";
echo $xml->book[1]->title['lang'];
?>

The yield of the code above will be:

WEB
en

PHP SimpleXML - Get Attribute Values - Loop

The following precedent gets the characteristic estimations of the <title> components in the "books.xml" file:

Example

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot make object");
foreach($xml->WEB() as $books) {
    reverberation $books->title['lang'];
    reverberation "<br>";
}
?>

The yield of the code above will be:

en
en
en-us
en-us