|
|
|
|
|
|
| |
TheFile.xml
| <?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.
?>
<TABLE CELLPADDING="0" CELLSPACING="1" BORDER="1" WIDTH="150" style="table-layout:fixed">
<TR ALIGN="LEFT">
<TD>
<A href="<?=$LinkURL[0]?>" target="_blank"><?=$Heading[0]?></A><BR>
<A href="<?=$LinkURL[0]?>"><?=$Body[0]?></A>
</TD>
</TR>
<TR ALIGN="LEFT">
<TD>
<A href="<?=$LinkURL[1]?>" target="_blank"><?=$Heading[1]?></A><BR>
<A href="<?=$LinkURL[1]?>"><?=$Body[1]?></A>
</TD>
</TR>
<TR ALIGN="LEFT">
<TD>
<A href="<?=$LinkURL[2]?>" target="_blank"><?=$Heading[2]?></A><BR>
<A href="<?=$LinkURL[2]?>"><?=$Body[2]?></A>
</TD>
</TR>
</TABLE> | | |
|
| RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | A very basic and fast XML parser Categories : PHP, PHP Classes, XML | | | Simple newsreader script Categories : PHP, XML, Rich Site Summary (RSS) | | | RSS parser using PHP5 and simpleXML Categories : Rich Site Summary (RSS), PHP, XML | | | Glossword - glossary compiler Categories : Content Management, PHP, MySQL, XML | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | XML easy parser Categories : PHP, XML | | | MySQL or SQL Query to XML Output Categories : PHP, MySQL, XML, Databases | | | XML Menu Categories : PHP, PHP Classes, Navigation, XML, XSL | | | Directory Listing To XML : Outputs XML File of a Given Directory Listing Categories : PHP, PHP Classes, XML, Directories | | | Trivia Quiz program using XML, XSLT and PHP Categories : PHP, XML, XSLT | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | SAPIPROCESSOR - is a compact XML-based CMS toolkit Categories : PHP, Content Management, XML | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | On-the-fly drop down menu from a txt or xml file Categories : PHP, XML, HTML and PHP | |
| | | | Strong Yuan wrote :997
how about this?so short and clear.
<TABLE CELLPADDING="0" CELLSPACING="1" BORDER="1" WIDTH="150" style="table-layout:fixed">
<?php
for ($i=0;$i<$Set;$i++){
echo "<TR ALIGN=LEFT>
<TD>
<A href=$LinkURL[$i] target=_blank>$Heading[$i]</A><BR>
<A href=$LinkURL[$i]>$Body[$i]</A>
</TD>
</TR>";
}
?>
</TABLE>
| |
|
|
|