<?xml version='1.0' ?>
<!DOCTYPE Listings [<!ELEMENT Listings (Listing+)>
<!ELEMENT Listing (LinkURL, Heading, Body)>
<!ATTLIST Listing BPid CDATA #REQUIRED>
<!ELEMENT LinkURL (#PCDATA)>
<!ELEMENT Heading (#PCDATA)>
<!ELEMENT Body (#PCDATA)>]>
<Listings>
<Listing BPid='10000201'>
<LinkURL><![CDATA[http://www.weberdev.com/]]></LinkURL>
<Heading><![CDATA[PHP and MySQL: WeberDev.com]]></Heading>
<Body><![CDATA[PHP and MySQL: WeberDev.com is dedicated to bring you the latest web
development resources, focusing on open source solutions and PHP and MySQL.]]></Body>
</Listing>
<Listing BPid='10000738'>
<LinkURL><![CDATA[http://www.weberdev.com/]]></LinkURL>
<Heading><![CDATA[PHP and MySQL: WeberDev.com]]></Heading>
<Body><![CDATA[PHP and MySQL: WeberDev.com is dedicated to bring you the latest web
development resources, focusing on open source solutions and PHP and MySQL.]]></Body>
</Listing>
<Listing BPid='10000994'>
<LinkURL><![CDATA[http://www.weberdev.com/]]></LinkURL>
<Heading><![CDATA[PHP and MySQL: WeberDev.com]]></Heading>
<Body><![CDATA[PHP and MySQL: WeberDev.com is dedicated to bring you the latest web
development resources, focusing on open source solutions and PHP and MySQL.]]></Body>
</Listing>
</Listings>
GetData.php
<?
//At this stage we define 3 arrays that will hold the CDATA info that we will get
//from the feed
$LinkURL = array();
$Heading = array();
$Body = array();
//Here we set the full path to the file or put a URL for the file
$file = "TheFile.xml";
//Will be used to tell us which CDATA info we are collecting at any given moment
$Type = "";
//The XML info is wrapped in a Listing. Each Listing holds a set of the 3 elements
//that we need to extract (LinkURL, Heading and Body). $Set Will be used to keep
//track of which Listing we are in. It will actually be used to tell us where in
//the arrays to put the data we are currently passing.
$Set=0;
//Any XML feed needs a startElement function. This function is called by the
//XML Parser any time a new element is parsed. It is less relevant for this
//specific example but in general if you have <TAG>Some info</TAG> then
//startElement will be called when the XML Parser will get to <TAG>.
//In this example we use this function to know on which tag we are, since
//we need to know if we are on LinkURL,Heading or Body. We Set the Global
//Variable Type to $name which is the name of the TAG.
function startElement($parser, $name, $attrs) {
global $Type;
$Type = $name;
}
//endElement is similar to startElement but is called for </TAG>.
//In this example we are dealing with CDATA so this function is
//not needed. We define it just cause we have to pass it to the
//XML parser.
function endElement($parser, $name) {
}
//This function is called by the XML Parser when it needs to deal
//with CDATA. As you can see, 3 If statements decide to which of
//the global arrays we created (LinkURL,Heading or Body) we set
//the data. Notice that all of the comparisons are done with CAPITAL
//versions of the names of the TAGS. After we set the BODY we know
//That we have finished to populate the 3 arrays for a specific
//Listing so we increment the $Set variable so that the next Listing
//will be in the next cell of each array.
function characterData($parser, $data) {
global $Type,$Set,$LinkURL,$Heading,$Body;
If($Type == "LINKURL") {
$LinkURL[$Set] = $data;
$Type="";
}ElseIf($Type == "HEADING") {
$Heading[$Set] = $data;
$Type="";
}ElseIf($Type == "BODY") {
$Body[$Set] = $data;
$Type="";
$Set++;
}
}
//Create a parser and set it's handler to $xml_parser
$xml_parser = xml_parser_create();
// Tell the Parser the names of the needed functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
// Tell the Parser the name of the function that handles CDATA
xml_set_character_data_handler($xml_parser, "characterData");
/.Open the XML file or URL
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
//Parse the data.
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
//At this stage we have 3 arrays with the information we need and we can format them as we like.
?>