[Phpxd-commits] CVS: phpXD/samples/cfg2xml Cfg2xml.php,NONE,1.1
Status: Beta
Brought to you by:
growbal
|
From: Growbal K. <gr...@us...> - 2002-03-08 06:54:35
|
Update of /cvsroot/phpxd/phpXD/samples/cfg2xml
In directory usw-pr-cvs1:/tmp/cvs-serv25305/samples/cfg2xml
Added Files:
Cfg2xml.php
Log Message:
Add sample files: samples/cfg2xml/cfg2xml.php samples/files/fielddef1.cfg
--- NEW FILE: Cfg2xml.php ---
<?php
// phpXMLDOM - an XML DOM Implementation
//
// This Software and all associated files are released unter the
// GNU Public License (GPL), see LICENSE for details.
//
// $Id: Cfg2xml.php,v 1.1 2002/03/08 06:54:32 growbal Exp $
/**
* Sample PHP script for using phpXMLDOM. Converts the file ../files/fielddef1.cfg
* to fielddef1.xml and prints it out.
*
* @package phpXD
* @author Growbal Kuo <gr...@ta...>
* @version $Revision: 1.1 $
*/
define( "PHPXD_INCLUDE_DIR", "d:/dev/cvs/phpXD/" );
include( PHPXD_INCLUDE_DIR . "phpXD.php" );
//===========================================================
/**
* Gets the value of cfg file.<br>
*
*
*
* @param $str <code>string</code>
* @param $col <code>int</code>
* @returns string
*/
function getColumn($str, $col) {
$str = trim($str);
$str = trim(stristr($str, " "));
$arr = split(",",$str);
if ( $col >= count($arr))
return "";
return trim($arr[$col]);
}
//===========================================================
function convertFile($FieldDefFile) {
if (! ($file= fopen($FieldDefFile, "r"))) {
echo("file FieldDefFile open error");
exit();
} else {
$doc = new Document();
$root =& $doc->createElement("fields");
$lines = file($FieldDefFile);
while (list($key, $value) = each($lines)) {
$ele =& convertLine ( $value, $doc);
if (get_class ($ele)=="element" ) {
$root->appendChild( $ele );
}
}
fclose($file);
$doc->appendChild($root);
}
return $doc;
}
function newElement($tagname, $content, $doc) {
$ret =& $doc->createElement($tagname);
if (trim($content)!="") {
$text = new Text();
$text -> setData($content);
$ret->appendChild($text);
}
return $ret;
}
function convertLine($line, $doc) {
//... definition of cfg file
// fieldtypeForInput: 1:simple text, 2:text with select menu 3:textArea 4:only menu 5:teeth
// varType: 0:decimal, 1:text
// sumType: 0:no sum, 1:simple expression, 2:lab data(justify with max and min),
// 3: fields with menu 4: urine cells data 5:+ ++ +++... 6: only + -
// fieldUnit: unit of the field or other description or instruction
// will display on the screen at the tail of the field
$fieldTypeArr = array (
"1" => "simple_text",
"2" => "menu_text",
"3" => "text_area",
"4" => "menu",
"5" => "teeth"
);
$varTypeArr = array(
"0" => "decimal",
"1" => "text"
);
$sumTypeArr = array(
"0" => "no_sum",
"1" => "simple_sum",
"2" => "lab",
"3" => "menu",
"4" => "urine_cell",
"5" => "multiple+-",
"6" => "single+-"
);
if (strtolower(substr(trim($line), 0, 6)) == "field ") {
$caption = getColumn($line, 0);
$maxLen = getColumn($line, 1);
$fieldName = strtoupper( getColumn($line, 2) );
$fieldType = $fieldTypeArr[ getColumn($line, 3) ];
$varType = $varTypeArr[ getColumn($line, 4) ];
$sumType = $sumTypeArr[ getColumn($line, 5) ];
$fieldUnit = getColumn($line, 6);
$defaultValue = $defaultValue;
$labStd["M"]["upper"] = getColumn($line, 8);
$labStd["M"]["lower"] = getColumn($line, 9);
$labStd["F"]["upper"] = getColumn($line, 10);
$labStd["F"]["lower"] = getColumn($line, 11);
$sumText = $caption;
$field_ele =& newElement("field", "", $doc);
$field_ele ->setAttribute( "name", $fieldName );
$field_ele ->appendChild( newElement( "caption" , $caption, $doc));
$field_ele ->appendChild( newElement( "sumText", $caption, $doc));
$field_ele ->appendChild( newElement( "maxLen", $maxLen, $doc));
$field_ele ->appendChild( newElement( "fieldtype", $fieldType, $doc));
$field_ele ->appendChild( newElement( "varType", $varType , $doc));
$field_ele ->appendChild( newElement( "sumType", $sumType, $doc));
$field_ele ->appendChild( newElement( "fieldUnit", $fieldUnit, $doc));
$defaultValue_ele =& $field_ele->appendChild( newElement("defaultValue","", $doc));
$defaultValue_ele->appendChild( newElement( "common", $defaultValue,$doc) );
if ($sumType=="lab") {
$limit_ele =& newElement("limit", "", $doc);
if ($labStd["M"]["upper"]==$labStd["F"]["upper"] && $labStd["M"]["lower"]==$labStd["F"]["lower"]) {
$lower_ele =& newElement("lower", "", $doc);
$lower_ele->appendChild(newElement("official", $labStd["M"]["lower"], $doc));
$upper_ele =& newElement("upper", "", $doc);
$upper_ele->appendChild(newElement("official", $labStd["M"]["upper"], $doc));
$common_ele =& newElement("common", "", $doc);
$common_ele->appendChild($lower_ele);
$common_ele->appendChild($upper_ele);
$limit_ele->appendChild($common_ele);
}
else {
$lower_ele =& newElement("lower", "", $doc);
$lower_ele->appendChild(newElement("official", $labStd["M"]["lower"], $doc));
$upper_ele =& newElement("upper", "", $doc);
$upper_ele->appendChild(newElement("official", $labStd["M"]["upper"], $doc));
$male_ele =& newElement("male", "", $doc);
$male_ele->appendChild($lower_ele);
$male_ele->appendChild($upper_ele);
$limit_ele->appendChild($male_ele);
$lower_ele =& newElement("lower", "", $doc);
$lower_ele->appendChild(newElement("official", $labStd["F"]["lower"], $doc));
$upper_ele =& newElement("upper", "", $doc);
$upper_ele->appendChild(newElement("official", $labStd["F"]["upper"], $doc));
$female_ele =& newElement("female", "", $doc);
$female_ele->appendChild($lower_ele);
$female_ele->appendChild($upper_ele);
$limit_ele->appendChild($female_ele);
}
$field_ele->appendChild( $limit_ele);
}
}
else
$field_ele = null;
return $field_ele;
}
//==================================
$filename = "../files/fielddef1";
//... build a DOM Document object from a file
$document =& convertFile($filename.".cfg");
//... build a DOMWriter object , set the encoding
$dw = new DOMWriter();
$dw->encoding = "Big5";
//... write output to file test.xml
$dw->writeNode($filename.".xml", $document);
//... write output to stdout
$dw->writeNode("php://stdout", $document);
//... write output to stdout
//echo $dw->writeToString($document);
?>
|