PHP SimpleXML

View: 606    Dowload: 0   Comment: 0   Post by: admin   Category: Web Development   Fields: none

6 point/3 review File has been tested

PHP SimpleXML handles the most common XML tasks and leaves the rest for other extensions.

What is PHP SimpleXML?

SimpleXML is new in PHP 5.

The SimpleXML extension provides is a simple way of getting an XML element's name and text.

Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an XML element.

SimpleXML converts the XML document (or XML string) into an object, like this:

  • Elements are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they are placed inside an array
  • Attributes are accessed using associative arrays, where an index corresponds to the attribute name
  • Text inside elements is converted to strings. If an element has more than one text node, they will be arranged in the order they are found

SimpleXML is fast and easy to use when performing tasks like:

  • Reading/Extracting data from XML files/strings
  • Editing text nodes or attributes

However, when dealing with advanced XML, you are better off using the Expat parser or the XML DOM.

Installation

As of PHP 5, the SimpleXML functions are part of the PHP core. No installation is required to use these functions.

PHP SimpleXML Examples

Assume we have the following XML file, "note.xml":

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> 

Now we want to output different information from the XML file above:

Example 1

Output keys and elements of the $xml variable (which is a SimpleXMLElement object):

<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?> 

The output of the code above will be:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! ) 

Example 2

Output the data from each element in the XML file:

 <?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>

The output of the code above will be:

Tove
Jani
Reminder
Don't forget me this weekend! 

Example 3

Output the element's name and data for each child node:

 <?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br>";
  }
?>

The output of the code above will be:

note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend! 

More PHP SimpleXML

For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.

PHP SimpleXML

PHP SimpleXML Posted on 20-03-2014  PHP SimpleXML handles the most common XML tasks and leaves the rest for other extensions What is PHP SimpleXML? SimpleXML is new in PHP 5.The SimpleXML extension provides is a simple way of getting an XML element's name and text. 2/10 606

Comment:

To comment you must be logged in members.

Files with category

  • .NET Core 2.0, Angular 4 and MySQL. Delete Actions

    .NET Core 2.0, Angular 4 and MySQL. Delete Actions

    View: 41    Download: 0   Comment: 0

    Category: ASP.NET Framework     Fields: Other

    To finalize the coding part of the series, we are going to implement the logic for deleting the entity. To do that, we are going to create a form to support this action and send the DELETE request towards our server.

  • WPF Binding Window/UserControl to Self

    WPF Binding Window/UserControl to Self

    View: 29    Download: 0   Comment: 0

    Category: Web Development     Fields: Other

    I recently started a project where in order to bind to properties within the Code section of UserControl (or Window), they were using DependencyProperty definitions, in fact sometimes it was even worse, they were also deriving from...

  • Workflow Manager using Unity config File in ASP.NET

    Workflow Manager using Unity config File in ASP.NET

    View: 212    Download: 0   Comment: 0

    Category: ASP.NET Framework     Fields: Other

    This article explains a design approach that involves setting the work flow of the process using config file and Unity Framework. It helps to change the flow only by updating the config file and no other code change is required.

  • SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    View: 114    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays...

  • ASP.NET Core 2.0 User Role Base Dynamic Menu Management Using Dependency Injection

    ASP.NET Core 2.0 User Role Base Dynamic Menu Management Using Dependency Injection

    View: 45    Download: 0   Comment: 0

    Category: ASP.NET Framework     Fields: Other

    In this article we will see in detail how to display role-based dynamic menu after a user logs in. For this we will create a Menu Master table and insert a few records to display the menu and link the URL to the menu based on the logged in user's...

  • A beginner's guide to URL rewriting, with plenty of examples.

    A beginner's guide to URL rewriting, with plenty of examples.

    View: 46    Download: 0   Comment: 0

    Category: Web Development     Fields: none

    URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site. It can also be the source of near-unending misery and suffering. Definitely worth playing carefully with it - lots of testing is...

  • How to use the PHP download file script? Class Download PHP

    How to use the PHP download file script? Class Download PHP

    View: 83    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    This is my favorite PHP download script. I’ve used a different more simple method until a client wanted to be able to allow their site visitors to download a large file from a password protected directory. The PHP script works on Apache web servers...

  • Data Scraping from Image using Tesseract

    Data Scraping from Image using Tesseract

    View: 31    Download: 0   Comment: 0

    Category: Web Development     Fields: Other

    Data Science is a growing field. According to CRISP DM model and other Data Mining models, we need to collect data before mining out knowledge and conduct predictive analysis. Data Collection can involve data scraping, which includes web scraping...

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd