php提取xml

    技术2025-02-19  60

    1.有一个demo.xml文件

    2.利用php代码提取demo.xml里的一些信息并生成一个网页

       a.提取demo.xml所有field tag的相关属性

         例如<field name="abcd" x=10pt y=10pt w=10pt h=10pt>

         需要得到name,x,y,w,h(x,y表示左上角坐标,w,h表示宽高)

       b.遇到<break这个tag表示分页用<hr>

    3.当把所有的field属性提取完毕之后,再根据这些属性生成一个html网页,网页的内容就是把field所在的矩形框画出来(x,y,w,h),网页左上角为原点

    4.当用鼠标点击这些矩形框时,在矩形框上显示field的name属性。

     

    代码如下:

     

    <?php     ob_start();     $xmlfile = 'quest_433_file_1_template_1_14674.pdf.xml';     $xmlparser = xml_parser_create();     $fp = fopen($xmlfile, 'r');     $xmldata = fread($fp, abs(filesize($xmlfile)));      //$filesize=filesize($xmlfile);     //$array = explode('<break',$xmldata);     xml_parse_into_struct($xmlparser,$xmldata,$xmlarr);     //print_r($xmlarr);     xml_parser_free($xmlparser);      echo "<html>";         //print_r($xmlarr[1713]['attributes']);         echo "<head>";         for($i=0;$i<count($xmlarr);$i++)         {             if(array_key_exists("attributes",$xmlarr[$i])&&array_key_exists("NAME",$xmlarr[$i]['attributes'])&&array_key_exists("X",$xmlarr[$i]['attributes'])&&array_key_exists("Y",$xmlarr[$i]['attributes'])&&array_key_exists("W",$xmlarr[$i]['attributes'])&&array_key_exists("H",$xmlarr[$i]['attributes']))             {                 echo "<style type=text/css>table.";                 echo $xmlarr[$i]['attributes']['NAME'].$i."{positon:absolute;left:";                 echo "border=1;";                 echo "left:".$xmlarr[$i]['attributes']['X'].";";                 echo "top:".$xmlarr[$i]['attributes']['Y'].";";                 echo "width:".$xmlarr[$i]['attributes']['W'].";";                 echo "height:".$xmlarr[$i]['attributes']['H']."}";                 echo "</style>";             }         }         echo "</head><body width=612pt height=792pt>";         //print_r($xmlarr);         for($j=0;$j<count($xmlarr);$j++)         {             if($xmlarr[$j]['tag']=="BREAK")                 echo "<hr size=5 width=100%>";             if(array_key_exists("attributes",$xmlarr[$j])&&array_key_exists("NAME",$xmlarr[$j]['attributes'])&&array_key_exists("X",$xmlarr[$j]['attributes'])&&array_key_exists("Y",$xmlarr[$j]['attributes'])&&array_key_exists("W",$xmlarr[$j]['attributes'])&&array_key_exists("H",$xmlarr[$j]['attributes']))             {                 echo "<script language=/"JavaScript/">";                 echo "function a".$j."_onclick()";                 echo "{";                 echo "if(this.document.getElementById(/"".$xmlarr[$j]['attributes']['NAME'].$j."/").style.display==/"none/")";                 echo "this.document.getElementById(/"".$xmlarr[$j]['attributes']['NAME'].$j."/").style.display=/"table-row/"; ";                 echo " else ";                 echo "this.document.getElementById(/"".$xmlarr[$j]['attributes']['NAME'].$j."/").style.display=/"none/";";                 echo "return true;";                 echo "}</script>";                 echo "<div οnclick=/"return a".$j."_onclick()/">";                 echo "<table border=1  class=";                 echo $xmlarr[$j]['attributes']['NAME'].$j."><tr><td><a id=".$xmlarr[$j]['attributes']['NAME'].$j." style=/"display:none;/">".$xmlarr[$j]['attributes']['NAME']."</a></td></tr></table></div>";             }         }         echo "</body></html>";     $o_fname = 'output.html';      //$length = ob_get_length();     $buffer = ob_get_contents();     ob_end_clean();     $fp = fopen($o_fname,"w+");     fwrite($fp,$buffer);            fclose($fp); //} ?>

    最新回复(0)