Jump to content

Xeek

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Xeek's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I have to reference the PHP manual and web-sites still, but I try to piece things together into something I can use. Well, anyways, I am trying to use the xml functions I am able to use. Unfortunately the host I'm writing this script on does not have PHP5, rather 4.4.7, so I have to use this. Well as noob as I am, I managed to realize my coding errors and I came up with this. It's the output I'm confused about. Here is a php file that contains a couple classes I want to reuse elsewhere. If you think you can improve it somehow, let me know. Below, I'll describe another file that accesses it, but it's the output I'm confused about. <?php //xmlparser.php class xml { // $the_xml = new xml("blah.xml","showmeopentag","showmedata","showmeclosetag"); var $parser; var $vOT; var $vXD; var $vCT; function xml($file,$open_tag,$xdata,$close_tag) { //initialize vars $this->vOT = $open_tag; $this->vXD = $xdata; $this->vCT = $close_tag; // create the parser object $this->parser = xml_parser_create(); // set the calls xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, "tag_open", "tag_close"); xml_set_character_data_handler($this->parser, "cdata"); // open the xml file if (!($fp = fopen($file, "r"))) { die("Could not open XML input"); } // read and parse the xml while ($data = fread($fp, 4096)) { if (!xml_parse($this->parser, $data, feof($fp))) { die(sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } } fclose($fp); // free the parser xml_parser_free($this->parser); } function tag_open($parser, $tag, $attributes) { call_user_func($this->vOT,$parser,$tag,$attributes); } function cdata($parser, $cdata) { call_user_func($this->vXD,$parser,$cdata); } function tag_close($parser, $tag) { call_user_func($this->vCT,$parser,$tag); } } class xml_write { var $fp; var $ind; function xml_write($file) { $this->fp = fopen($file, "w"); fwrite($this->fp, "<?xml version='1.0' standalone='yes'?>\n"); } function tag_open($tag, $attributes) { if ($attributes) { fwrite($this->fp, str_repeat(" ", $this->ind)."<".$tag." ".$attributes.">\n"); } else { fwrite($this->fp, str_repeat(" ", $this->ind)."<".$tag.">\n"); } $indent++; } function cdata($cdata) { if (!strpos($cdata, "\n")) { fwrite($this->fp, str_repeat(" ", $this->ind).$cdata); } else { fwrite($this->fp, "\n".str_repeat(" ", $this->ind).$cdata."\n"); } } function tag_close($tag) { $indent--; fwrite($this->fp, str_repeat(" ", $this->ind)."</".$tag.">\n"); } function finalize() { fclose($this->fp); } } ?> (side question..) How do I make a function optionally accept an argument? <?php // test.php require("xmlparse.php"); $a = new xml_write("test.xml"); $a->tag_open("books",""); $a->tag_open("book",""); $a->tag_open("author",""); $a->cdata("r.l. stein"); $a->tag_close("author"); $a->tag_close("book"); $a->tag_close("books"); $a->finalize(); $a = new xml("test.xml","oT","xD","cT"); function oT($a,$b,$c) { echo "oT: ".$a." ".$b." ".$c."<br>"; } function xD($a,$b) { echo "xD: ".$a." ".$b."<br>"; } function cT($a,$b) { echo "cT: ".$a." ".$b."<br>"; } ?> It outputs the xml file just fine except the indentation, but at least with the correct lower/upper case. It's what appears on the web-page that I'm confused about: oT: Resource id #4 BOOKS Array xD: Resource id #4 oT: Resource id #4 BOOK Array xD: Resource id #4 oT: Resource id #4 AUTHOR Array xD: Resource id #4 xD: Resource id #4 r.l. stein xD: Resource id #4 cT: Resource id #4 AUTHOR xD: Resource id #4 cT: Resource id #4 BOOK xD: Resource id #4 cT: Resource id #4 BOOKS I don't really care about the resource id part, but why are my tags (or xml elements?) coming out uppercase and why is the data coming with correct case? The attributes are outputed as an array? How do I work with that? In any case, I think this is a good xml parser example Help is appreciated!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.