Need some help with parsing an XML file

Reply

Join Date: Feb 2006
Posts: 1
Reputation: Dactyl is an unknown quantity at this point 
Solved Threads: 0
Dactyl Dactyl is offline Offline
Newbie Poster

Need some help with parsing an XML file

 
0
  #1
Feb 18th, 2006
Hello everyone

I am learning how to parse XML files with PHP. And I cant figure out how to do something. Any help is greatly appreciated

Here is a sample XML file that I am trying to parse:
<?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
<?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);
}
}
}
}
?>
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 48
Reputation: RamiroS is an unknown quantity at this point 
Solved Threads: 0
RamiroS's Avatar
RamiroS RamiroS is offline Offline
Light Poster

Re: Need some help with parsing an XML file

 
0
  #2
Feb 24th, 2006
You can try the XML_Serializer if you can use PEAR: http://pear.php.net/package/XML_Serializer

It will let your convert XML 2 Array and Array 2 XML. Great tool.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Need some help with parsing an XML file

 
0
  #3
Mar 4th, 2006
Check out my class_xml.php. It should make this job easy for you.
http://www.troywolf.com/articles/php/class_xml/

Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC