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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now