Save 100s of Dev Hours - Get The React Codebase Generator

Array to Xml Conversion and Xml to Array Convert in PHP

array to xml to array

Hi Geeks,
Today i am sharing you a code snippet for converting xml to array with attributes also and vice versa (Convert array to xml). This class is very useful to parse rss feed and to make rss feed in php programming.

In this code snippet we have made a class to convert array XML using php code and also retaining its attibues stored into array . It returns the XML in form of DOMDocument class for further manipulation. This class will throw exception if the tag name or attribute name has illegal characters.

Usage of this class:

A. Steps for Convert Array to Xml

1.Create a class named Array2XML with following code in file Array2XML.php

2. Now Include the class

3. Defining Array or get it from database then call the createXML method for generating xml file

Now Call createXML method as following

B. Steps for Convert Xml to Array

1. Define the xml oe get the xml string into a varribale

2.Now call the XML_TO_ARR function as following

You May Also Like

About the Author: Jeetendra Singh

Jeetendra Singh is a UI Architect who has 10+ Years of Experience as Full Stack Developer. He is a Microsoft certified technology Specialist and AWS Certified Associate Architect. His Major Interests are in Mongo Db, Express Js, React Js, Node Js (MERN STACK). He build variety of applications using Mern, Mean, Node, JavaScript & jQuery