WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Extracting CDATA values from an XML feed / file.
Categories : PHP, XML
Boaz Yahav
Date : Jun 28th 2003
Grade : 4 of 5 (graded 6 times)
Viewed : 26929
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Boaz Yahav
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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>



XML To Array
Categories : PHP, PHP Classes, XML, Arrays
XML easy parser
Categories : PHP, XML
RSS parser using PHP5 and simpleXML
Categories : Rich Site Summary (RSS), PHP, XML
MySQL to XML.
Categories : MySQL, XML, PHP
php Free chat simple fast and customizable chat server that uses a simple filesystem for message and nickname storage
Categories : PHP, AJAX, XML, Complete Programs
Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
SAPIPROCESSOR - is a compact XML-based CMS toolkit
Categories : PHP, Content Management, XML
XML Menu
Categories : PHP, PHP Classes, Navigation, XML, XSL
A very basic and fast XML parser
Categories : PHP, PHP Classes, XML
utf8_decode -- Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1.
Categories : PHP, PHP Functions, XML
TAB_STRUCT Class: Is supporting Class for the DBXML Class
Categories : PHP, PHP Classes, MySQL, XML, Databases
logger class (PHP5 +)
Categories : PHP, PHP Classes, Log Files, XML
XMLManipulation
Categories : PHP, XML, SimpleXML
Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +)
Categories : PHP, Ecommerce, XML, Web Services, CURL
 Strong Yuan wrote :997
how about this?so short and clear.

&lt;TABLE CELLPADDING="0" CELLSPACING="1" BORDER="1" WIDTH="150" style="table-layout:fixed"&gt; 
&lt;?php
for ($i=0;$i&lt;$Set;$i++){
echo "&lt;TR ALIGN=LEFT&gt; 
        &lt;TD&gt; 
           &lt;A href=$LinkURL[$i] target=_blank&gt;$Heading[$i]&lt;/A&gt;&lt;BR&gt; 
           &lt;A href=$LinkURL[$i]&gt;$Body[$i]&lt;/A&gt; 
        &lt;/TD&gt; 
    &lt;/TR&gt;";
}
?&gt;
&lt;/TABLE&gt;