Posts: 115
Location: in the interwebz
|
Hello all,
I am working on making a small CMS for a personal project.The cms will use a simple xml document with the following format.
Code:
<?xml version="1.0"?>
<root>
<page><id>1</id>
<title>Sample Date</title>
<description>This is the description</description>
<content>This is some content</content>
</page>
<page> <id>2</id>
<title>Sample Content 2</title>
<description>This is the description</description>
<content>This is some content</content>
</page>
</root>
At this point, I can get information, but I am unable to choose which piece I want. My thought was to find the node from the start tag, add a marker and send that to the actual node.
Somehow, I think this logic is flawed. I would like to be able to search the document by id, and then work on displaying the content contained under that id.
PHP Code:
$file = "database.xml";
function defaultHandler($parser, $text){ echo $text .'default';
} function startElement($parser, $name, $text){ $val = '2';
$name = strtolower($name); if ($name == 'id') { $x = "1"; print $x; return $x; } else $x = "0"; print $x; return $x; }
function text ($parser, $text, $x){ print $x; if(preg_match("/\S/", $text)) { } }
function endElement($parser, $name){
}
$xml_parser_object = xml_parser_create(); xml_set_element_handler($xml_parser_object, "startElement", "endElement"); xml_set_character_data_handler($xml_parser_object, "text"); xml_set_default_handler($xml_parser_object, "defaultHandler");
if (!$handle = fopen($file, "r")) { die("Error."); } while ($xml_data = fread($handle, 1024)) { if (!xml_parse($xml_parser_object, $xml_data, feof($handle))){ die(sprintf("Error %s on line %d", xml_error_string(xml_get_error_code($xml_parser_object)), xml_get_current_line_number($xml_parser_object))); } }
xml_parser_free($xml_parser_object); fclose($handle);
|