Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Converting XML Into A PHP Data Structure
A Look At The XML File Structure
XML files are designed to be validated against a DTD and store data in a format similar to something you'd see in an HTML document. All tags are just made up on the fly (as defined by a DTD) and can represent a tree structure. Here is an example of some XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- This is just a comment, ignore it -->
    <drive desc="Letters and Numbers Harddrive">
        <folder name="folder01">
            <file name="a.txt"/>
            <file name="b.txt"></file>
        </folder>
        <folder name="folder02">
            <file name="c.txt"/>
            <file name="d.txt" owner="bob">
            This is a comment about file d.
            We like comments.</file>
        </folder>
    </drive>
Now, if you look at this document, you'll notice that there exists one <drive> tag with two folder tags inside it. Also, each folder tag contains two file tags within them. This file creates a tree-like structure of data. Now, we would like to access this data from within PHP. There are many ways to get to this data from within PHP including:
  • manually parsing the XML file,
  • parsing the file with the PHP SAX parser,
  • using the XPath libraries to search and pull data,
  • or using the DOM parser
The manual option is not our most robust solution, and the DOM support in PHP is still experimental. So, I've chosen to use the SAX parser route. However, unlike similar other solutions, I'd like to write an object in PHP that parses this XML document into a PHP data structure so that I can access the data like any other PHP data instead of having to write a custom parser each time I use XML in an application.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
function calling from java scriptlampsunil08/02/05 05:31
RE: Xml to Array, another simple solution...ans04/29/05 06:57
Another optionEdemilson Lima01/15/04 13:19
looking for solutionlakshman12/19/03 05:40
Excellent job!escobar09/16/03 13:09
computernorman02/28/03 05:59
Thanks for the excellent articleRob Keniger02/11/03 09:31
xml2array+array2xml alternativeTulpe02/03/03 08:35
Use the best option... Benjamin Smith02/03/03 00:59
Xml to Array, another simple solution...marcoBR01/10/03 20:51
XML module for PHPPhillip.01/10/03 13:27
Well doneSam12/30/02 16:01
ArrayRGJ12/27/02 08:53
Great article!Tormod12/25/02 11:38
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.