I have been told that using XML with PHP is ineffcient as it is slow and uses alot of memory. Being a mediocre programmer and little experience with "industrial size" development applications and sites I state that my little dabblings into the XML realm is pretty useful. I have developed alot of small scaled applications in which i needed an easy way of handling data and being able to distributing it between apps and environments. I have never really thought about its efficiency and would like some feed back on it as I intend on using alot of XML as an alternative for some common tasks in terms of data handling and accessing.
I had a theory of using XML to store menu information structures as i have had alot of pain when trying to get menus/ submenus from a database which is flat and trying to format it into a html menu structure. I am hoping to use it so that accessability of specific information is more easier...(being able to get submenus directly instead of tedious loop throughs and traverse processing)
How efficient is using XML in PHP? what should it be mainly used for? what can be done to "lighten" the load and make it more robust? (i.e. simpler xml files?)
PHP's XML effeciency
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP's XML effeciency
Is using XML solving your problem is a way that works well for you? Are you having performance problems? Honestly, there are many very useful sub-systems that are "inefficient as it is slow and uses a lot of memory." By that metric none of us should use databases.
There are faster solutions for some kinds of data than XML, but if what you are using is working for you -- what is the problem? And if you abstract the interface to your data source then you will be able to swap how the data is stored for something faster if you do have performance problems...
(#10850)
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: PHP's XML effeciency
Well I'm not seeing a problem at the moment. Infact I think it has worked for me alot. However, my opinion is baised and based on the fact that my apps have been rather "small" and I'm thinking....would this be a "practical approach now for a much larger project?". you know sometimes you get some little gimmick and you run with it as much as you can till you come across a situation that has you like..."uhm...this isn't working anymore"...I don't want to get too comfortable into thinking that XML is going to be the definite solution and would atleast try to do some research to see how feasible implementations like this will work in "larger" situations.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP's XML effeciency
I don't think there is a "definite solution." When you do a larger application you will find out and deal with it then.
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: PHP's XML effeciency
XML can be memory intensive yes.
You basically have two standard options when dealing with XML:
1) SAX parser
2) DOM tree
The first one is far less memory intensive as it basically just traverse through an XML document and using callbacks allows a programmer to save details about a node or completely ignore them. This is useful for things like using XML files for configuration storage, when it's not really the structure you are worried about just the data.
DOM is what you are likely using, SimpleXML and DOMDocument?
The former, is a native PHP array the latter is likely an optimized internal array.
Native PHP arrays have the advatange of being easy to work with but come at the expense of "probably" consuming more memory (garbage collection, etc).
I would guess that the DOM is like similar to MySQL resultsets. If you want to efficiently handle large amounts of data with taxing the system in memory it's probably best to keep the result set as a native MyQSL resultset and pull each result out row by row. If you dumped each result into a native PHP array, although it's easier to work with, you would likely cause more memory to be used.
What are you using the XML for exactly? If your dealing with thousands of records, absolutely use a RDBMS. Build a conversion utility to import/export as XML if portability is really that important.
If the document data has an a good amount of hierarchy then maybe you should consider using XML. If your just storing linear records though, it's far easier to use a database.
Cheers
You basically have two standard options when dealing with XML:
1) SAX parser
2) DOM tree
The first one is far less memory intensive as it basically just traverse through an XML document and using callbacks allows a programmer to save details about a node or completely ignore them. This is useful for things like using XML files for configuration storage, when it's not really the structure you are worried about just the data.
DOM is what you are likely using, SimpleXML and DOMDocument?
The former, is a native PHP array the latter is likely an optimized internal array.
Native PHP arrays have the advatange of being easy to work with but come at the expense of "probably" consuming more memory (garbage collection, etc).
I would guess that the DOM is like similar to MySQL resultsets. If you want to efficiently handle large amounts of data with taxing the system in memory it's probably best to keep the result set as a native MyQSL resultset and pull each result out row by row. If you dumped each result into a native PHP array, although it's easier to work with, you would likely cause more memory to be used.
What are you using the XML for exactly? If your dealing with thousands of records, absolutely use a RDBMS. Build a conversion utility to import/export as XML if portability is really that important.
If the document data has an a good amount of hierarchy then maybe you should consider using XML. If your just storing linear records though, it's far easier to use a database.
Cheers
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: PHP's XML effeciency
[quote='Hockey']DOM is what you are likely using, SimpleXML and DOMDocument?[/quote]
Yeah I like the accessability of the tree using SimpleXML...
[quote='Hockey']This is useful for things like using XML files for configuration storage, when it's not really the structure you are worried about just the data.[/quote]
I am comtemplating this move in conjunction with OOP. In a case of storing website navigation in a database and retrieving it to ouput a webpage navigation the XML structure plays a part here...
[quote='Hockey']What are you using the XML for exactly? If your dealing with thousands of records, absolutely use a RDBMS. Build a conversion utility to import/export as XML if portability is really that important.[/quote]
Well I am dealing with thousands of records. However my use fo the xml in this scenario comes in when dealing with view a details page...I'll keep heavy detail content in an xml (for instance content for articles) so instead of hitting the database to retrieve the contents I will just retrieve an id and load the xml..However I'm probably re-considering using some ajax or xslt to parse and output the data rather than simplexml. I'm thinking since i need RSS it would be an oppurtunity to utilize the xml file in both instances...where i can just do a loop through and include() the xml file into an output process.
I'm guess kinda looking at combine my knowledge of xml and skills of php into something that i can adopt as my own style of developing robust applications...and i want to research this as best as possbile before i go charing 6 million dollars to people who want to use my apps
P.S. I was joking about the 6 million
Yeah I like the accessability of the tree using SimpleXML...
[quote='Hockey']This is useful for things like using XML files for configuration storage, when it's not really the structure you are worried about just the data.[/quote]
I am comtemplating this move in conjunction with OOP. In a case of storing website navigation in a database and retrieving it to ouput a webpage navigation the XML structure plays a part here...
[quote='Hockey']What are you using the XML for exactly? If your dealing with thousands of records, absolutely use a RDBMS. Build a conversion utility to import/export as XML if portability is really that important.[/quote]
Well I am dealing with thousands of records. However my use fo the xml in this scenario comes in when dealing with view a details page...I'll keep heavy detail content in an xml (for instance content for articles) so instead of hitting the database to retrieve the contents I will just retrieve an id and load the xml..However I'm probably re-considering using some ajax or xslt to parse and output the data rather than simplexml. I'm thinking since i need RSS it would be an oppurtunity to utilize the xml file in both instances...where i can just do a loop through and include() the xml file into an output process.
I'm guess kinda looking at combine my knowledge of xml and skills of php into something that i can adopt as my own style of developing robust applications...and i want to research this as best as possbile before i go charing 6 million dollars to people who want to use my apps
P.S. I was joking about the 6 million
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: PHP's XML effeciency
If you are storing the XHTML for articles, etc you could do one of two things to speed things up:
1) Store each article as an indivudal file; quasi xhtml file that is included into a XHTML template -- this is what I do
2) Use SAX to locate the single record at which point you need to extract the details required and save it in a custom format
The former is probably much faster and way easier to implement.
SAX is useful if you want to conduct a quick search on XML files and extract a single record though.
1) Store each article as an indivudal file; quasi xhtml file that is included into a XHTML template -- this is what I do
2) Use SAX to locate the single record at which point you need to extract the details required and save it in a custom format
The former is probably much faster and way easier to implement.
SAX is useful if you want to conduct a quick search on XML files and extract a single record though.
- kendall
- Forum Regular
- Posts: 852
- Joined: Tue Jul 30, 2002 10:21 am
- Location: Trinidad, West Indies
- Contact:
Re: PHP's XML effeciency
Well wow thanks for your recommendation cause its EXACTLY what I theorized.Hockey wrote:If you are storing the XHTML for articles, etc you could do one of two things to speed things up:
1) Store each article as an indivudal file; quasi xhtml file that is included into a XHTML template -- this is what I do
2) Use SAX to locate the single record at which point you need to extract the details required and save it in a custom format
The former is probably much faster and way easier to implement.
SAX is useful if you want to conduct a quick search on XML files and extract a single record though.
I was trying to implement some sort of search mechanism for the xml but i reverted back to using a database....hey...thats what makes databases great.
what is SAX though...never heard of it.