Location: PHPKode > projects > Tinbox > contentadmin/parsexml.php
<?php
// XML parser - parses file to $parsedXML;

function parsexml($title,$file)
        {
        global $URLlink,$qa,$ta,$fi,$parsedXML, $mod;

        $URLlink=0; //toggle for link tag for special treatment of charachter data
        $qa=0; // number of qa tags (questions)
        $ta=1; // numbering of ta tags (tables)
        $fi=0; // numbering of fi tags (figures titles)
        $mod=0; // numbering of mod tags (modules)

        $parsedXML = ""; //initialise
        $xml_parser = xml_parser_create();
        xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($xml_parser, "characterData");

        if (!($fp = fopen($file, "r")))
                die("could not open XML input");

        while ($data = fread($fp, 4096))
                if (!xml_parse($xml_parser, $data, feof($fp)))
                        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));

        xml_parser_free($xml_parser);
        fclose($fp);
        //return $parsedXML;
        }

function startElement($parser, $name, $attrs)
        {
        global $URLlink, $mod, $modidx ,$qa, $figURL, $parsedXML, $ta, $qa, $fi, $fig, $image, $tfile, $XMLfile;
        if($name=="mod")
                $mod++;
        if($name=="fi")
                $fi++;
        if($mod==$modidx)
                {
                switch($name)
                        {
                        //<info>
                        case "info":$parsedXML .= "<hr><h1>"; break;
                        case "docnumber":$parsedXML .= "<table class=\"box\" width=\"100%\"><tr><td><b>Doc No: </b>"; break;
                        case "revision":$parsedXML .= "<b>Revision: </b>"; break;
                        case "category":$parsedXML .= "<b>Category: </b>"; break;
                        case "subcategory":$parsedXML .= "<b>Sub-Category: </b>"; break;
                        case "target":$parsedXML .= "</td><td><b>Targeted at: </b>"; break;
                        case "level":$parsedXML .= "<b>Level: </b>"; break;
                        case "author":$parsedXML .= "<b>Author: </b>"; break;
                        case "copyright":$parsedXML .= "<b>Copyright: </b>"; break;
                        case "legal":$parsedXML .= "<b>Legal Information: </b>"; break;
                        //</info>
                        // <info> & <mod>
                        case "title": break;
                        case "abstract": $parsedXML .= "<p><b>"; break;
                        //<mod>
                        case "mod": $parsedXML .= "<h1>".$mod.". "; break;
                        case "h1": $parsedXML .= "<h2 class=\"tagh1\"           >"; break;
                        case "h2": $parsedXML .= "<h3>"; break;
                        case "ta": $parsedXML .= "<center><table width=\"50%\" frame=\"border\" cellspacing=\"0\" class=\"tabular\"><caption align=\"bottom\">Table ".$ta++."</caption>"; break;
                        case "tr": $parsedXML .= "<tr>";break;
                        case "td": $parsedXML .= "<td>";break;
                        case "th": $parsedXML .= "<th>";break;
                        case "ul": $parsedXML .= "<ul>";break;
                        case "ol": $parsedXML .= "<ol>";break;
                        case "li": $parsedXML .= "<li>";break;
                        case "dl": $parsedXML .= "<dl>";break;
                        case "dt": $parsedXML .= "<dt>";break;
                        case "dd": $parsedXML .= "<dd>";break;
                        case "al": $parsedXML .= "<ul>";break;
                        case "at": $parsedXML .= "<li><b>";break;
                        case "ad": $parsedXML .= " -";break;
                        case "fi": $parsedXML .= "<table class=\"figure\" width=\"350\" frame=\"border\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td valign=\"top\" rowspan=\"2\"><FORM ENCTYPE=\"multipart/form-data\" ACTION=\"\" METHOD=POST>Edit this image: <br><INPUT size =\"10\" NAME=\"imagefile\" TYPE=\"file\"><br><INPUT TYPE=\"submit\" VALUE=\"Update\"><INPUT TYPE=\"hidden\" NAME=\"XMLfile\" VALUE=\"".$XMLfile."\"><INPUT TYPE=\"hidden\" NAME=\"fig\" VALUE=\"".$fi."\"></form></td><th><img src=\"figure/?filename=".$XMLfile."&fig=".$fi."\" alt=\"Figure ".$fi."\"></th></tr><tr><td><b>Figure ".$fi.". </b>"; break;
                        case "co": $parsedXML .= "<pre>"; break;
                        case "te": $parsedXML .= "<pre>"; break;
                        case "qa": $parsedXML .= "<center><table width=\"500\" frame=\"border\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"question\">"; $qa++; break;
                        case "qu": $parsedXML .= "<tr><th colspan=\"2\" align=\"left\">Q".$qa.". "; break;
                        case "qw": $parsedXML .= "<tr><td nowrap width=\"2%\"><input type=\"radio\" name=\"Q[".$qa."]\" value=\"0\"></td><td>- "; break;
                        case "qr": $parsedXML .= "<tr><td nowrap width=\"2%\"><input type=\"radio\" name=\"Q[".$qa."]\" value=\"1\"></td><td>-<b> "; break;
                        case "wa": $parsedXML .= "<table width=\"95%\" frame=\"border\" class=\"keyword\"><tr><th width=\"10\"><img src=\"icons/warning.gif\" alt=\"Warning\"></th><th>Warning</th></tr><tr><td width=\"10\"></td><td>"; break;
                        case "im": $parsedXML .= "<table width=\"95%\" frame=\"border\" class=\"keyword\"><tr><th width=\"10\"><img src=\"icons/important.gif\" alt=\"Important\"></th><th>Important</th></tr><tr><td width=\"10\"></td><td>"; break;
                        case "ca": $parsedXML .= "<table width=\"95%\" frame=\"border\" class=\"keyword\"><tr><th width=\"10\"><img src=\"icons/caution.gif\" alt=\"Caution\"></th><th>Caution</th></tr><tr><td width=\"10\"></td><td>"; break;
                        case "no": $parsedXML .= "<table width=\"95%\" frame=\"border\" class=\"keyword\"><tr><th width=\"10\"><img src=\"icons/note.gif\" alt=\"Note\"></th><th>Note</th></tr><tr><td width=\"10\"></td><td>"; break;
                        case "ti": $parsedXML .= "<table width=\"95%\" frame=\"border\" class=\"keyword\"><tr><th width=\"10\"><img src=\"icons/tip.gif\" alt=\"Tip\"></th><th>Tip</th></tr><tr><td width=\"10\"></td><td>"; break;
                        case "pa": $parsedXML .= "<p>"; break;
                        case "a":  $parsedXML .= "<cite>"; break;
                        case "e":  $parsedXML .= "<b>"; break;
                        case "i":  $parsedXML .= "<kbd>"; break;
                        case "o":  $parsedXML .= "<samp>"; break;
                        case "u":  $parsedXML .= "<a href=\""; $URLlink=1; break;
                        case "png":
                        case "gif":
                        case "jpg": $image=1;break;
                        }
                }
        }

function endElement($parser, $name)
        {
        global $modidx, $mod, $qa, $parsedXML, $qa, $image;
        if($mod==$modidx)
                {
                switch($name)
                        {
                        //<info>
                        case "info": break;
                        case "docnumber":$parsedXML .= "<br>"; break;
                        case "revision":$parsedXML .= "<br>"; break;
                        case "category":$parsedXML .= "<br>"; break;
                        case "subcategory":$parsedXML .= "<br>"; break;
                        case "target":$parsedXML .= "<br>"; break;
                        case "level":$parsedXML .= "<br>"; break;
                        case "author":$parsedXML .= "<br>"; break;
                        case "copyright":$parsedXML .= "<br>"; break;
                        case "legal":$parsedXML .= "</td></tr></table><br>"; break;
                        //</info>
                        // <info> & <mod>
                        case "title": $parsedXML .= "</h1>"; break;
                        case "abstract": $parsedXML .= "</b></p>"; break;
                        //<mod>
                        case "mod": $parsedXML .= "<hr>"; break;
                        case "h1": $parsedXML .= "</h2>"; break;
                        case "h2": $parsedXML .= "</h3>"; break;
                        case "ta": $parsedXML .= "</table></center>"; break;
                        case "tr": $parsedXML .= "</tr>";break;
                        case "td": $parsedXML .= "</td>";break;
                        case "th": $parsedXML .= "</th>";break;
                        case "ul": $parsedXML .= "</ul>";break;
                        case "ol": $parsedXML .= "</ol>";break;
                        case "li": $parsedXML .= "</li>";break;
                        case "dl": $parsedXML .= "</dl>";break;
                        case "dt": $parsedXML .= "</dt>";break;
                        case "dd": $parsedXML .= "</dd>";break;
                        case "al": $parsedXML .= "</ul>";break;
                        case "at": $parsedXML .= "</b>";break;
                        case "ad": $parsedXML .= "</li>";break;
                        case "fi": $parsedXML .= "</td></tr></table>"; break;
                        case "co": $parsedXML .= "</pre>"; break;
                        case "te": $parsedXML .= "</pre>"; break;
                        case "qa": $parsedXML .= "</td></tr></table></center>"; break;
                        case "qu": $parsedXML .= "</th></tr>"; break;
                        case "qw": $parsedXML .= "</td></tr>"; break;
                        case "qr": $parsedXML .= "</b></td></tr>"; break;
                        case "wa": $parsedXML .= "</td></tr></table>"; break;
                        case "im": $parsedXML .= "</td></tr></table>"; break;
                        case "ca": $parsedXML .= "</td></tr></table>"; break;
                        case "no": $parsedXML .= "</td></tr></table>"; break;
                        case "ti": $parsedXML .= "</td></tr></table>"; break;
                        case "pa": $parsedXML .= "</p>"; break;
                        case "a": $parsedXML .= "</cite>"; break;
                        case "e": $parsedXML .= "</b>"; break;
                        case "i": $parsedXML .= "</kbd>"; break;
                        case "o": $parsedXML .= "</samp>"; break;
                        case "u": $parsedXML .= "</a>"; break;
                        case "png":
                        case "gif":
                        case "jpg": $image=0;break;
                        }
                }
        }

function characterData($parser, $data)
        {
        global $URLlink, $parsedXML, $image, $mod, $modidx;
        if($mod==$modidx)
                {
                if (!$URLlink)
                        {
                        if (!$image)
                               $parsedXML .= htmlentities($data);
                        }
                else
                        {
                        $parsedXML .= $data."\">".$data;
                        $URLlink=0;
                        }
                }
        }
?>
Return current item: Tinbox