Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 14th, 2004, 1:14 PM   #1
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Is there an easy-to-use, understandable way to parse XML in PHP? I have tried writing my own parser, but I cannot find a file-reading function that leaves tags intact.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 14th, 2004, 2:30 PM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Hey,

Have you taken a look at this->www.php.net/xml

It's all the parser functions for xml.

if this isn't what you are looking for, post here again and i will keep looking.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Oct 14th, 2004, 2:48 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I've seen it before. It's great, if you know what any of it means - I'm still a PHP newbie.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 14th, 2004, 3:15 PM   #4
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Ahh, ok. Well, study up a little, once you get the base stuff for php in your mind the rest falls like a house of cards.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Oct 14th, 2004, 3:20 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Well, I figured out the bits I need to fiddle with - all I need to know now is how to access the attributes in the startElement function. $attrs[x] doesn't work.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 14th, 2004, 5:05 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I figured it out. I was trying to create a navigation treeview. The code is below, in case anybody's interested.

Here's the PHP parser:
<?php
$file = "nav.xml";
$link = false;

function startElement($parser, $name, $attrs) {
	global $link;
	
	switch ($name) {
 case "MENU":
 	echo '<div id="'.$attrs["ID"].'" class="menu">';
 	echo '<img src="images/expand.gif" id="'.$attrs["ID"].'button" width="12" height="12" onclick="toggleMenu(\''.$attrs["ID"].'\');" alt="" class="showhide" /> ';
 	
 	if ($attrs["LINK"])
  echo '<a href="'.$attrs["LINK"].'">'.$attrs["CAPTION"].'</a>';
 	else
  echo $attrs["CAPTION"];
 	
 	echo '<div id="'.$attrs["ID"].'items" class="menuitems">';
 	break;
 case "ITEM":
 	if ($attrs["LINK"]) {
  echo '<a href="'.$attrs["LINK"].'">';
  $link = true;
 	}
 	else
  $link = false;
 	break;
	}
}

function endElement($parser, $name) {
	global $link;
	
	switch ($name) {
 case "MENU":
 	echo '</div>';
 	echo '</div>';
 	break;
 case "ITEM":
 	if ($link)
  echo '</a>';
 	echo '<br />';
 	break;
	}
}

function characterData($parser, $data) {
	echo $data;
}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
@ $fp = fopen($file, "r");
if (!($fp))
	echo '<p class="naverror">We are unable to load the navigation bar. We apologise for the inconvenience.</p>';
else {
	while ($data = fread($fp, 4096)) {
 if (!xml_parse($xml_parser, $data, feof($fp))) {
 	sprintf("XML error: %s at line %d",
  	xml_error_string(xml_get_error_code($xml_parser)),
  	xml_get_current_line_number($xml_parser));
  	break;
 }
	}
	xml_parser_free($xml_parser);
}
?>

And here's the XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE nav SYSTEM "nav.dtd">

<navigation>

<menu id="menu1" caption="Menu 1">
	<menu id="menu1sub1" caption="Submenu 1">
 <item link="#">Submenu Item 1</item>
 <item>Submenu Item 2</item>
 <item>Submenu Item 3</item>
	</menu>
	<item>Menu Item 1</item>
	<item>Menu Item 2</item>
	<item>Menu Item 3</item>
</menu>

<menu id="menu2" caption="Menu 2" link="#">
	<item link="#">Menu Item 1</item>
	<item link="#">Menu Item 2</item>
	<item link="#">Menu Item 3</item>
</menu>

<menu id="menu3" caption="Menu 3">
	<item>Menu Item 1</item>
	<item>Menu Item 2</item>
	<item>Menu Item 3</item>
</menu>

</navigation>
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 14th, 2004, 9:20 PM   #7
dnathe4th
Programmer
 
Join Date: Oct 2004
Posts: 32
Rep Power: 0 dnathe4th is on a distinguished road
very neat, i never thought of that application,
where are you using that?
__________________
&lt;CENTER&gt;<span style='font-size:17pt;line-height:100%'>My Homepage</span>&lt;/CENTER&gt;
dnathe4th is offline   Reply With Quote
Old Oct 15th, 2004, 5:27 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
For my school website. It's currently hosted at http://olaves.thefryhole.co.uk/index.php - the original is at http://www.saintolaves.net/.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Oct 16th, 2004, 3:21 PM   #9
BlazingWolf
Hobbyist Programmer
 
Join Date: Sep 2004
Posts: 207
Rep Power: 5 BlazingWolf is on a distinguished road
I must say that is very cool, and innovative.
__________________
_______________________________
BlazingWolf
BlazingWolf is offline   Reply With Quote
Old Oct 20th, 2004, 2:46 AM   #10
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
PHP has EXTENSIVE support for XML (though not as much as Perl). It would be sensless to write your own XML parser.... dare I say re-inventing the wheel?
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC