php提取xml

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值