Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Difference between simplexml_load_file and simplexml_load_string

I want to put a xml file into my programme and make it into an array so I can put it into a table.

I was wondering how I can do this and I have read the php manual, but I don't seem to be able to get to grips with it.

To do what I want, do I need to use simplexml_load_string, or do I need to command both of them(simplexml_load_file and simplexml_load_string), therefore load the xml file onto the programme, then turn it into a file. Or does simplesml_load_string just does all of that for me.

Also I was wondering what get_object_vars does when you put it around an array.

like image 709
Masayuki Tonoki Avatar asked Aug 12 '15 02:08

Masayuki Tonoki

People also ask

What is SimpleXML in web technology?

SimpleXML is an extension that allows us to easily manipulate and get XML data. 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.

How do I create a SimpleXML object?

Example #2 Create a SimpleXMLElement object from a URL$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE); echo $sxe->asXML();

What is Cdata PHP?

C Data Handles ¶C array elements can be accessed as regular PHP array elements, e.g. $cdata[$offset] C arrays can be iterated using foreach statements. C arrays can be used as arguments of count(). C pointers can be dereferenced as arrays, e.g. $cdata[0]

1 Answers

Each of the two functions returns SimpleXMLElement object, so you only need one of them depending on what you have to begin with, whether you have path to an XML file or content of the XML it self in a string variable, etc.

Notice in the documentation page, that simplexml_load_string() expects the string parameter to contain the actual XML content, while simplexml_load_file() expects the string parameter to contain path to the XML file.

like image 171
har07 Avatar answered Oct 13 '22 00:10