XML Expat Parser

Posted on 2008-10-20
Last Modified: 2013-12-13
I'm fairly new to PHP, and I'm looking for some sample Expat code that rolls the following XML into a PHP array:

<Casetrack RelativeMonth="1" Day="1"/>
<Casetrack RelativeMonth="1" Day="2"/>
<Casetrack RelativeMonth="1" Day="3"/>
<Casetrack RelativeMonth="1" Day="7"/>
<Casetrack RelativeMonth="1" Day="8"/>

(I did not omit the DTD, it doesn't exist.)
Question by:jdana
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Assisted Solution

thebradnetwork earned 83 total points
ID: 22763715
Here is a good link for you to learn more about PHP.

What is that code suppose do do?
LVL 13

Assisted Solution

Xyptilon2 earned 83 total points
ID: 22764076
You can load the XML into an array with the php function SimpleXMLElement(), simplexml_load_string() or simplexml_load_file() function.

$string = <<<XML
<?xml version='1.0'?>
<Casetrack RelativeMonth="1" Day="1"/>
<Casetrack RelativeMonth="1" Day="2"/>
<Casetrack RelativeMonth="1" Day="3"/>
<Casetrack RelativeMonth="1" Day="7"/>
<Casetrack RelativeMonth="1" Day="8"/>

$xml = simplexml_load_string($string);


The variabele $xml will contain the XML data over which you can then loop or do other things. Goodluck, i hope this is useful. For more information see:
LVL 25

Accepted Solution

Marcus Bointon earned 84 total points
ID: 22765333
Once you have parsed the XML as Xyptilon2 suggests, you then want to extract the parts you want. I'd suggest that XPath is the best way to do that, and SimpleXML has support for it:
XPath syntax is here:

You would probably want something like this:

$data = $xml->xpath('Casetrack');

Author Closing Comment

ID: 31507788
Thanks for the advice.  I'll actually got the Expat parser working with my array.  It's not easy to learn, but once you get it, it's pretty powerful.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question