: : g_XML OUTPUT
Balíček slouží ke snadnému výstupu do xml souborů.
Použití:
Pro nejrychlejší použití je k dispozici Globalni XML, ktere staci jendou inicializovat:
<?
/*
<xml>
<title>nadpis</title>
<data sub="1" attribut="dalsi" />
</xml>
*/
Global_XML();
// inicializuje globalni XML objekt
OpenSection("xml");
// otevre root element <xml>
AppendValue("title", "nadpis");
// prida hodnotu <title>nadpis</title>
OpenSection("data");
// otevre sekci <data>
AppendAttr("sub", "1");
// prida <data sub="1" />
AppendAttr("attribut", "dalsi");
// prida <data sub="1" attribut="dalsi"
CloseSection("data");
// uzavre otevrenou sekci data
CloseSection("xml");
// uzavre sekci </xml>
echo XmlReturnData();
// vypise vystupni xml
?>Kód:
g_xml.php
<?
/*
OpenSection($name, $value='', $XML=0);
AppendAttr($name, $value, $XML=0);
AppendValue($name, $value, $XML=0);
CloseSection($name, $XML=0);
XmlReturnData($XML = 0)
*/
function Global_XML() {
return $GLOBALS['_XML_Out'] =& new Xml_Output;
}
function PlaceChar($chr, $count) {
$ret = '';
for ($i = 0; $i < $count; $i++) {
$ret .= $chr;
}
return $ret;
}
// ------------------------------------------------------------------------------ //
// XML ELEMENT CLASS
// ------------------------------------------------------------------------------ //
class Xml_Element {
var $parent;
var $XML;
var $first_elem;
var $last_elem;
var $next;
var $type;
var $name;
var $value;
function Xml_Element() {
$this->type = 0;
$this->name = '';
$this->value = '';
}
function Clear() {
$this->parent = 0;
$this->first_elem = 0;
$this->last_elem = 0;
$this->next = 0;
}
function AddSubs(&$elem) {
if ($this->first_elem == 0) {
$this->first_elem =& $elem;
} else
if ($this->last_elem != 0) {
$this->last_elem->next =& $elem;
}
$this->last_elem =& $elem;
$elem->XML =& $this->XML;
}
function Output($ident) {
$idnt = $ident;
if ($this->type == 0) {
$ret = PlaceChar(' ',$ident).'<'.$this->name;
$vals = '';
$idnt = $ident + $this->XML->default_ident;
} else {
$ret = ' '.$this->name.'="'.htmlspecialchars($this->value).'"';
return $ret;
}
$elem = $this->first_elem;
while($elem != 0) {
if ($elem->type == 1) {
$ret .= $elem->Output(0);
} else {
if (empty($vals)) $vals = "\n";
$vals .= $elem->Output($idnt);
}
$elem = $elem->next;
}
if (empty($this->value) && empty($vals)) {
$ret .= ' />'."\n";
} else {
$ret .= '>'.htmlspecialchars($this->value).(!empty($vals) ? $vals.PlaceChar(' ',$ident) : '').'</'.$this->name.'>'."\n";
}
return $ret;
}
function Count() {
$ret = 1;
$elem = $this->first_elem;
while($elem != 0) {
$ret += $elem->Count();
$elem = $elem->next;
}
return $ret;
}
}
// ------------------------------------------------------------------------------ //
// XML OUTPUT CLASS
// ------------------------------------------------------------------------------ //
class Xml_Output {
var $root_section;
var $last_section;
var $default_ident;
function Xml_Output() {
$this->root_section = 0;
$this->last_section = 0;
$this->default_ident = 2;
}
function xml() {
if ($this->root_section != 0)
return $this->root_section->Output(0);
return '';
}
function OpenSection($name, $value = '') {
$new_section = new Xml_Element();
$new_section->name = $name;
$new_section->value = $value;
$new_section->type = 0;
$new_section->Clear();
if ($this->root_section == 0) {
$this->root_section =& $new_section;
$this->root_section->parent = 0;
$this->root_section->XML =& $this;
} else
if ($this->last_section != 0) {
$new_section->parent =& $this->last_section;
$this->last_section->AddSubs($new_section);
}
$this->last_section =& $new_section;
return $this->last_section;
}
function Attr($name, $value) {
$this->AppendAttribute($name, $value);
}
function AppendAttribute($name, $value) {
$new_section = new Xml_Element();
$new_section->name = $name;
$new_section->value = $value;
$new_section->type = 1;
if ($this->last_section != 0) {
$new_section->parent =& $this->last_section;
$this->last_section->AddSubs($new_section);
}
}
function CloseSection($name = '') {
if ($this->last_section != 0) {
$lname = $this->last_section->name;
$this->last_section =& $this->last_section->parent;
if (!empty($name) && strtoupper($lname) != strtoupper($name)) {
$this->CloseSection($name);
}
}
}
function CloseAll() {
if ($this->last_section != 0)
while ($this->last_section->parent != 0)
CloseSection();
}
function Count() {
if ($this->root_section != 0)
return $this->root_section->Count();
return 0;
}
}
function OpenSection($name, $value='', $XML=0) {
if (empty($XML) && isset($GLOBALS['_XML_Out'])) $XML =& $GLOBALS['_XML_Out'];
if ($XML)
$XML->OpenSection($name, $value);
}
function CloseSection($name='', $XML=0) {
if (empty($XML) && isset($GLOBALS['_XML_Out'])) $XML =& $GLOBALS['_XML_Out'];
if ($XML)
$XML->CloseSection($name);
}
function AppendAttr($name, $value, $XML=0) {
if (empty($XML) && isset($GLOBALS['_XML_Out'])) $XML =& $GLOBALS['_XML_Out'];
if ($XML)
$XML->AppendAttribute($name, $value);
}
function AppendValue($name, $value, $XML=0) {
if (empty($XML) && isset($GLOBALS['_XML_Out'])) $XML =& $GLOBALS['_XML_Out'];
if ($XML) {
$XML->OpenSection($name, $value);
$XML->CloseSection($name);
}
}
function XmlReturnData($XML = 0) {
if (empty($XML) && isset($GLOBALS['_XML_Out'])) $XML =& $GLOBALS['_XML_Out'];
if ($XML)
return $XML->xml();
return '';
}
function OuputArray(&$XML, $ardat) {
reset($ardat);
while(list($naz, $val) = each($ardat)) {
if (is_numeric($val)) {
$XML->Attr($naz, $val);
} else {
if (!empty($val)) {
$XML->OpenSection($naz, $val);
$XML->CloseSection();
}
}
}
}
?>