Published in: PHP
************************************************************************ Example: ************************************************************************ $xml = new XML('artikel.xml'); $xml->print_nodes(); ?> *********************************************************************** Script *********************************************************************** <?php # This script formats a xml file according to some rules. These rules # are processed in the function that xml_set_character_data_handler # defines. We have to remember, in which element we are, and furthermore, # which ancestor elements the current element has. class Node { var $name; var $attributes; var $ancestors = "/" var $data; var $type; function Node($tree) { } function add_data($value) { $this->data .= ' '.$value; } function get_type() { return "with CDATA" } else { return "without CDATA" } } function level() { if ($this->ancestors == "/") return 0; } else { return 0; } } function has_attributes() { } function print_name() { return "$this->name"; } function is_child($node) { if ($node->ancestors == $this->ancestors) $result = false; return $result; } } class XML { var $file; var $PIs; var $format_body = "font-family:Verdana;font-size:10pt;" var $format_bracket = "color:blue;" var $format_element = "font-family:Verdana;font-weight:bold;font-size:10pt;" var $format_attribute = "font-family:Courier;font-size:10pt;" var $format_data = "font-size:12pt;" var $format_attribute_name = "color:#444444;" var $format_attribute_value = "font-family:Courier;font-size:10pt;color:red;" var $format_blanks = " " function XML($filename) { $this->file = $filename; # Why should one want to use case-folding with XML? XML is case-sensitiv, I think this is nonsense } } } } function startElement($parser, $name, $attribs) { # Adding the additional element to the tree, including attributes $this->tree[] = $name; $node = new Node($this->tree); $node->attributes[$k] = $v; } $this->nodes[] = $node; } function endElement($parser, $name) { # Adding a new element, describing the end of the tag # But only, if the Tag has CDATA in it! # Check $this->nodes[] = new Node($this->tree, NULL); } else { # Adding a slash to the end of the prev_node $prev_node->name = $prev_node->name."/" } } # Removing the element from the tree } function characterData($parser, $data) { } function process_instruction($parser, $target, $data) { if (preg_match("/xml:stylesheet/", $target, $match) && preg_match("/type=\"text\/xsl\"/", $data, $match)) { # print "<b>found xls pi: $PIs[1]</b><br>\n" } } function print_nodes() { # Printing the header # Printing the XML Data $node = $this->nodes[$i]; # Checking: Empty element $end_char = "/" } else { $end_char = "" } # Writing whitespaces, but only if it's _no_ closing element that follows # directly on it's opening element if (!("/".$this->nodes[$i-1]->name == $node->name)) { } echo "<span style=\"".$this->format_bracket."\"><</span><span style=\"".$this->format_element."\">".$node->name."</span>" if ($node->has_attributes()) { printf(" <span style=\"%s\">%s=\"</span><span style=\"%s\">%s</span><span style=\"%s\">\"</span>", $this->format_attribute_name, $keys[$j], $this->format_attribute_value, $node->attributes[$keys[$j]], $this->format_attribute_name); } echo " " } echo "<span style=\"".$this->format_element."\">$end_char</span><span style=\"".$this->format_bracket."\">></span>" } # Printing the footer print "</body></html>\n" } }
Comments
Subscribe to comments
You need to login to post a comment.

You forgot to add like 100 ;
I don`t understant wher umport php file