TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-18-2006, 06:08 PM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2005
Posts: 13
Thanks: 0
Chayron is on a distinguished road
Smile Need some help with parsing an XML file

Hello everyone

I am learning how to parse XML files with PHP. And I cant figure out how to do something.

Here is a sample XML file that I am trying to parse:
Quote:
<?xml version="1.0"?>
<emailsList>
<email no="1">
<from>
whatever@yahoo.com
</from>
<to>
yyy@yahoo.com
</to>
<subject>
Just te test
</subject>
<body>
Helloooooo
</body>
</email>
<email no="2">
<from>
whatever@hotmail.com
</from>
<to>
yyy@hotmail.com
</to>
<subject>
Just to test2
</subject>
<body>
Hiiiiiiii
</body>
</email>
</emailsList>
I am trying to parse this file so if no.1 is entered the information from the 1st node is shown. How can I do this? any help is appreciated.
Here is the php code to prase the xml document
Quote:
<?php

$xml = xml_parser_create();
$obj = new Parser_Object; // a class to assist with parsing

xml_set_object($xml,$obj);
xml_set_element_handler($xml, 'start_element', 'end_element');
xml_set_character_data_handler($xml, 'character_data');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);

$fp = fopen('data.xml', 'r') or die("Can't read XML data.");
while ($data = fread($fp, 4096)) {
xml_parse($xml, $data, feof($fp)) or die("Can't parse XML data");
}
fclose($fp);

xml_parser_free($xml);

class pc_RSS_item
{
var $from = '';
var $to = '';
var $subject = '';
var $body='';
function display() {
printf('<p><a href="%s">%s</a><br />%s</p>',
$this->link,htmlspecialchars($this->title),
htmlspecialchars($this->description));
}
}

class pc_RSS_parser
{
var $tag;
var $item;
function start_element($parser, $tag, $attributes)
{
if ('item' == $tag) {
$this->item = new pc_RSS_item;
} elseif (!empty($this->item)) {
$this->tag = $tag;
}
}
function end_element($parser, $tag) {
if ('item' == $tag) {
$this->item->display();
unset($this->item);
}
}
function character_data($parser, $data) {
if (!empty($this->item)) {
if (isset($this->item->{$this->tag})) {
$this->item->{$this->tag} .= trim($data);
}
}
}
}
?>
Chayron is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
ftp_fput() Please Help BlackKite Absolute Beginners 2 08-12-2006 09:47 PM
HTTP 500 error... kunoSTAR Absolute Beginners 15 08-05-2005 08:13 PM
PHP/MySQL tips and tricks jaswinder_rana Tips & Tricks 6 07-17-2005 01:56 AM


All times are GMT. The time now is 10:31 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design