php parse XML File SimpleXML

Today, We want to share with you php parse XML File SimpleXML.In this post we will show you How To Read XML file in PHP (SimpleXML), hear for how to get data from xml file in php we will give you demo and example for implement.In this post, we will learn about Generate XML file in PHP SimpleXML DOM Reader/Writer Method with an example.

php parse XML File SimpleXML

There are the Following The simple About php parse XML File SimpleXML Full Information With Example and source code.


As I will cover this Post with live Working example to develop php parse xml string, so the php read xml from url for this example is following below.

Read Also:  open new tab react Example with Tutorial

A Sample Data Of XML File

XML filename is products.xml

<?xml version="1.0"?>
<company>
	<productdtls>
		<productname>Mobile</productname>
		<title>Lenova</title>
		<productinfo>MD</productinfo>
		<price>500000</price>
	</productdtls>
	<productdtls>
		<productname>Laptop</productname>
		<title>Dell</title>
		<productinfo>CEO</productinfo>
		<price>250000</price>
	</productdtls>
</company>

Read Specific XML Elements in PHP

using simplexml_load_file function in PHP

<?php
$xmldata = simplexml_load_file("products.xml") or die("Failed to load");
echo $xmldata->productdtls[0]->productname . "<\n>";
echo $xmldata->productdtls[1]->productname; 
?>

Output:

Mobile
Laptop

PHP access all children of an object

Read XML Elements In A Loop using PHP

<?php
$xmldata = implexml_load_file("products.xml") or die("Failed to load");
foreach($xmldata->children() as $item) {         
	echo $item->productname . ", ";     
	echo $item->title . ", ";     
	echo $item->productinfo . ", ";        
	echo $item->price . "<\n>"; 
} 
?>

Results

Mobile, Lenova, MD, 500000
Laptop, Dell, CEO, 250000

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  SSL forcefully Redirect HTTP to HTTPS Using .htaccess File

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about php parse XML File SimpleXML.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.