#get xml file $xmlpath = dirname(__FILE__) . "\\"; $xmlfile = $xmlpath."report.xml"; if(!$dom = domxml_open_file($xmlpath."report.xml")) { echo "Error while parsing the document\n"; exit; } $PDF = &pc4p_create_pdf(array("author"=>"eTao System","title"=>"eTao report"));
$nodes = $dom->get_elements_by_tagname("//*"); foreach($nodes as $node) {
switch($node->tagname){ case "text": $instext = new Text($node); $instext->Draw($PDF); break; case "table":$instable = new Table($node); $instable->Draw($PDF); break; } } $PDF->pc4p_draw(); 第二部分:具体的在类中的实现 class Table extends PrintElement{ var $table; var $count,$pc; function Table($Table) { $this->table = $Table; } function Draw($PDF){ global $PDF; #get_attribute
| xx --table header
|----------------------------------
| yy --table body
|----------------------------------
| zz --table footer
|----------------------------------
有兴趣的话我把相关的文件样式mail给你.
<?php
print "<font size=-1 color=#ff00ff>节点名称</font>:(<font size=-1 color=red>节点属性</font>=<font size=-1 color=blue>节点属性的值</font>,[<font size=-1 color=red>节点属性</font>=<font size=-1 color=blue>节点属性的值</font>]) 节点值<br><br><br>";
$file = "testxml.xml";
$depth = array();function characterData($parser, $data) {
print $data;
}function startElement($parser, $name, $attrs) {
global $depth;
print "<br>";
for ($i = 0; $i < $depth[$parser]; $i++) {
print " ";
}
print "<font size=-1 color=#ff00ff>$name</font>:";
print "(";
while ( list( $key, $val ) = each( $attrs ) ) {
echo "<font color=red size=-1 >$key</font>=<font color=blue size=-1 >$val </font> ";
}
print ")";
xml_set_character_data_handler($parser, "characterData");
$depth[$parser]++;
}function endElement($parser, $name) {
global $depth;
$depth[$parser]--;
for ($i = 0; $i < $depth[$parser]; $i++) {
print " ";
}
print "<font size=-1 color=#ff00ff>/$name</font>";
print "<br>";
}$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
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);
?>
另外,我想请教一下,你知道如何用domxml技术实现吗?
有兴趣帮我完成上面这个模块吗?共同进步。
你可以重新编译你的php(linux)
加上 --with-dom --with-zlib
apache 也要重新编译一次
这样你就可以用了domxml中的函数了。
我还不知道这里的分有什么用的?你可以尝试着给100分我。
我帮你写出来。
:(((((
该模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。不知楼主为什么对此感兴趣!
$xmlfile = realpath("report.xml.txt");
$dom = xmldocfile($xmlfile);echo "<table border>";
$node = $dom->get_elements_by_tagname("//*");
foreach($node as $value) {
echo "<tr><td>",$value->tagname,"</td>";
echo "<td>";
if($att = $value->attributes())
foreach($att as $v)
echo $v->name,"=>",iconv("UTF-8","GB2312",$v->value),"<br>";
echo " </td>";
echo "<td>";
echo iconv("UTF-8","GB2312",$value->get_content());
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
switch($node->tagname){
case "text": $instext = new Text($node->attributes(),$node->get_content());
$instext->Draw($PDF);
break;
\\生成text实例,并且传入参数:1.控制文本字体的属性;2.具体的文本内容 case "table":$instable= new Table($node,$node->attributes());
$instable->Draw($PDF)
break;
\\生成table实例,并且传入参数:1.table这个节点,因为table这个节点还 有 子节点,所以在类中还需要进一步操纵其子节点;2.表格的属性
}
现在暂时就定为两个对象,其实还有其他的image等.
大家可能注意到了生成的实例都引用了两个同样的方法Draw($PDF),其中$PDF是一个全局变量,表示一个PDF文档的实例,所有的对象都是生成在PDF文档上的,php有支持PDF文档的类操作,在http://www.pc4p.net上面,大家可以下载.
第一部分:解析xml文件
<?include("Tagclass.php");?>
<?include("includes/pc4p/include/pc4p_init.inc")?>
<?
#get xml file
$xmlpath = dirname(__FILE__) . "\\";
$xmlfile = $xmlpath."report.xml";
if(!$dom = domxml_open_file($xmlpath."report.xml"))
{
echo "Error while parsing the document\n";
exit;
} $PDF = &pc4p_create_pdf(array("author"=>"eTao System","title"=>"eTao report"));
$nodes = $dom->get_elements_by_tagname("//*");
foreach($nodes as $node)
{
switch($node->tagname){
case "text": $instext = new Text($node);
$instext->Draw($PDF);
break;
case "table":$instable = new Table($node);
$instable->Draw($PDF);
break;
}
} $PDF->pc4p_draw();
第二部分:具体的在类中的实现
class Table extends PrintElement{ var $table;
var $count,$pc; function Table($Table)
{
$this->table = $Table;
} function Draw($PDF){ global $PDF;
#get_attribute
$table_x = $this->table->get_attribute("x");
$table_y = $this->table->get_attribute("y");
$table_border = $this->table->get_attribute("border");
$table_bordercolor = $this->table->get_attribute("bordercolor");
$table_maxlines = $this->table->get_attribute("maxlines");
$Page1 = &pc4p_create_page( $PDF, "a4" );
$Page1->pc4p_set_margin( array( "top" => 20, "bottom" => 20, "left" => 10, "right" => 10 ) );
$pageHeaderTable = &pc4p_create_object( $Page1, "table" );
$pageHeaderTable->pc4p_set_alignment ("center");
$pageHeaderTable->pc4p_create_tablematrix( 2 ,2);
$pageHeaderTable->pc4p_set_width( 600 );
$colArray=array(200,400);
$pageHeaderTable->pc4p_set_tablecolsize($colArray);
$pageHeaderTable->pc4p_set_tableborder("all","single", 2.0);
$tableName = &pc4p_create_object( $pageHeaderTable->cell[ 0 ][ 0 ] , "text" );
$tableName->pc4p_set_alignment( "center" );
//$tableName->pc4p_set_font("STSong-Light",18,"GBK-EUC-H");
$tableName->pc4p_set_textcolor( "red" );
$tableName->pc4p_set_text( "诺 贝 尔 瓷 砖" ); $tableName = &pc4p_create_object( $pageHeaderTable->cell[ 1 ][ 0 ] , "text" );
$tableName->pc4p_set_alignment( "center" );
//$tableName->pc4p_set_font("STSong-Light",18,"GBK-EUC-H");
$tableName->pc4p_set_textcolor( "red" );
$tableName->pc4p_set_text( "Johnshen" ); $tableName = &pc4p_create_object( $pageHeaderTable->cell[ 0 ][ 1 ] , "text" );
$tableName->pc4p_set_alignment( "center" );
//$tableName->pc4p_set_font("STSong-Light",18,"GBK-EUC-H");
$tableName->pc4p_set_textcolor( "red" );
$tableName->pc4p_set_text( "Johnshen" ); $line1 = &pc4p_create_object( $Page1, "line" );
$line1->pc4p_set_linestyle( "single" );
$line1->pc4p_set_width( "20%" );
$line1->pc4p_set_alignment( "center" );
$line1->pc4p_set_linecolor( "red" );
$line1->pc4p_set_margin( array( "top" => 2, "bottom" => 0, "left" => 10, "right" => 10 ) ); $Tablename = &pc4p_create_object( $Page1, "table" );
$Tablename->pc4p_set_alignment ("center");
$Tablename->pc4p_create_tablematrix( 4 );
$Tablename->pc4p_set_width( 570 );
$colArray=array(80,260,60,170);
$Tablename->pc4p_set_tablecolsize($colArray);
//$Tablename->pc4p_set_tableborder( "single" );
$namename= &pc4p_create_object( $Tablename->cell[ 0 ][ 0 ] , "text" );
$namename->pc4p_set_alignment( "left" );
//$namename->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$namename->pc4p_set_text( "|Customer Name" ); $namevalue= &pc4p_create_object( $Tablename->cell[ 1 ][ 0 ] , "text" );
$namevalue->pc4p_set_alignment( "left" );
//$namevalue->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$namevalue->pc4p_set_text( "|Ordern umber" );
$Table1 = &pc4p_create_object( $Page1, "table" );
$Table1->pc4p_set_alignment ("center");
$Table1->pc4p_create_tablematrix( 7 );
$Table1->pc4p_set_width( 570 );
$colArray=array(80,100,60,80,80,80,90);
$Table1->pc4p_set_tablecolsize($colArray);
$Table1->pc4p_set_tableborder("all","single", 1.0); $text1= &pc4p_create_object( $Table1->cell[ 0 ][ 0 ] , "text" );
$text1->pc4p_set_alignment( "left" );
//$text1->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$text1->pc4p_set_text( "|Po Type" ); $text2= &pc4p_create_object( $Table1->cell[ 1 ][ 0 ] , "text" );
$text2->pc4p_set_alignment( "left" );
//$text2->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$text2->pc4p_set_text( "|Po number" ); $text9a=&pc4p_create_object( $Table1->cell[ 2 ][ 0 ] , "text" );
$text9a->pc4p_set_alignment( "left" );
//$text9a->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$text9a->pc4p_set_text( "Unit" ); $text10a=&pc4p_create_object( $Table1->cell[ 3 ][ 0 ] , "text" );
$text10a->pc4p_set_alignment( "left" );
//$text10a->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$text10a->pc4p_set_text( "|Quantity" ); $text11=&pc4p_create_object( $Table1->cell[ 4 ][ 0 ] , "text" );
$text11->pc4p_set_alignment( "left" );
//$text11->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$text11->pc4p_set_text( "|Actual Qty" ); $text12=&pc4p_create_object( $Table1->cell[ 5 ][ 0 ] , "text" );
$text12->pc4p_set_alignment( "left" );
//$text12->pc4p_set_font("STSong-Light",13,"GBK-EUC-H");
$text12->pc4p_set_text( "|Comment" ); $Table2 = &pc4p_create_object( $Page1, "table" );
$Table2->pc4p_set_alignment ("center");
$Table2->pc4p_create_tablematrix( 1,$pageSize );
$Table2->pc4p_set_width( 570 );
}
这样你就可以看到数据输出到pdf文档中,现在的问题是,我这里的数据是固定的,现在要解决的是,如果根据传入的节点,依次遍历其子节点,并且取到相应的值.