![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() |
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! |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I've seen it before. It's great, if you know what any of it means - I'm still a PHP newbie.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() |
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! |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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> |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Oct 2004
Posts: 32
Rep Power: 0
![]() |
very neat, i never thought of that application,
where are you using that?
__________________
<CENTER><span style='font-size:17pt;line-height:100%'>My Homepage</span></CENTER> |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
For my school website. It's currently hosted at http://olaves.thefryhole.co.uk/index.php - the original is at http://www.saintolaves.net/.
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Sep 2004
Posts: 207
Rep Power: 5
![]() |
I must say that is very cool, and innovative.
![]()
__________________
_______________________________ BlazingWolf |
|
|
|
|
|
#10 |
|
Expert Programmer
|
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 <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|