Solved

XML Expat Parser

Posted on 2008-10-20
4
176 Views
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.)
0
Comment
Question by:jdana
4 Comments
 
LVL 6

Assisted Solution

by:thebradnetwork
thebradnetwork earned 83 total points
ID: 22763715
Here is a good link for you to learn more about PHP.
http://www.w3schools.com/php/default.asp

What is that code suppose do do?
0
 
LVL 13

Assisted Solution

by:Xyptilon2
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.

<?PHP
$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;

$xml = simplexml_load_string($string);

var_dump($xml);
?>

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: http://cn2.php.net/manual/en/function.simplexml-load-string.php
0
 
LVL 25

Accepted Solution

by:
Squinky 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:

http://uk2.php.net/manual/en/function.simplexml-element-xpath.php
XPath syntax is here:
http://www.w3schools.com/xpath/xpath_syntax.asp

You would probably want something like this:

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

Author Closing Comment

by:jdana
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 47
Not needed 13 57
Editing login page in zencart. 2 14
Setting Up Local Lan Web Server 1 16
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

20 Experts available now in Live!

Get 1:1 Help Now