[Phpxd-commits] CVS: phpXD/samples/writer1 writer1.php,NONE,1.1
Status: Beta
Brought to you by:
growbal
|
From: Thomas D. <th...@us...> - 2002-02-13 21:12:39
|
Update of /cvsroot/phpxd/phpXD/samples/writer1
In directory usw-pr-cvs1:/tmp/cvs-serv2640/samples/writer1
Added Files:
writer1.php
Log Message:
The beginning of more samples... :=)
--- NEW FILE: writer1.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: writer1.php,v 1.1 2002/02/13 21:12:25 thomi Exp $
/**
* Sample PHP script for using phpXMLDOM. Loads the file ../files/sample.xml
* and prints it as HTML.
*
* @package phpXD
* @author Thomas Dohmke <th...@do...>
* @version $Revision: 1.1 $
*/
require("../../phpXD.php");
function write(&$document, $trim = false) {
writeToString($document, $trim, $text);
echo $text;
}
function writeToString(&$dom, $deep = 0, &$text) {
$space = "";
for ($i = 0; $i < $deep; $i++) $space .= " ";
if ($dom->nodeType == CDATA_SECTION_NODE) {
$text .= $space;
$text .= "<span style=\"color:#990000\">";
$text .= "<![CDATA[";
$output = htmlspecialchars($dom->getData());
if (!(strpos($output, "\n") === false)) {
$break = "<br />";
}
else {
$break = "";
$space = "";
}
$text .= $break.$space;
$output = str_replace(" ", " ", $output);
$output = str_replace("\n", "<br />".$space, $output);
$output = str_replace("\t", " ", $output);
$text .= $output;
$text .= $break.$space;
$text .= "]]><br />";
$text .= "</span>";
}
if ($dom->nodeType == COMMENT_NODE) {
$text .= $space;
$text .= "<span style=\"color:#990099\">";
$text .= "<!--";
$output = htmlspecialchars($dom->getData());
if (!(strpos($output, "\n") === false)) {
$break = "<br />";
}
else {
$break = "";
$space = "";
}
$text .= $break.$space;
$output = str_replace(" ", " ", $output);
$output = str_replace("\n", "<br />".$space, $output);
$output = str_replace("\t", " ", $output);
$text .= $output;
$text .= $break.$space;
$text .= "--><br />";
$text .= "</span>";
}
if ($dom->nodeType == DOCUMENT_NODE) {
$text .= "<pre>";
$text .= "<span style=\"color:#009900\">";
$text .= "<?xml version=\"1.0\"?><br />";
$text .= "</span>";
if (!empty($dom->doctype)) {
if (!empty($dom->doctype->publicId) && !empty($dom->doctype->systemId)) {
$text .= "<!DOCTYPE ".$dom->doctype->name." PUBLIC ".
$dom->doctype->publicId." ".$dom->doctype->systemId;
}
else {
if (!empty($dom->doctype->systemId)) {
$text .= "<!DOCTYPE ".$dom->doctype->name." SYSTEM ".
$dom->doctype->systemId;
}
else {
$text .= "<!DOCTYPE ".$dom->doctype->name;
}
}
$text .= " (...)";
$text .= ">\n";
}
if ($dom->hasChildNodes()) {
writeToString($dom->firstChild, $deep, $text);
}
$text .= "</pre>";
}
if ($dom->nodeType == DOCUMENT_FRAGMENT_NODE) {
writeToString($dom->firstChild, $deep, $text);
}
if ($dom->nodeType == ELEMENT_NODE) {
$text .= $space;
$text .= "<span style=\"color:#000099\">";
$text .= "<".$dom->tagName;
if (isset($dom->attributes)) {
for ($i = 0; $i < $dom->attributes->getLength(); $i++) {
$elem =& $dom->attributes->item($i);
if ($elem->specified == true) {
$text .= " ".$elem->getName()."=\"".$elem->getValue()."\"";
}
}
}
if ($dom->hasChildNodes()) {
$text .= "><br />";
$text .= "</span>";
writeToString($dom->firstChild, $deep+4, $text);
$text .= $space;
$text .= "<span style=\"color:#000099\">";
$text .= "</".$dom->tagName."><br />";
$text .= "</span>";
}
else {
$text .= " /><br />";
$text .= "</span>";
}
}
if ($dom->nodeType == ENTITY_REFERENCE_NODE) {
if ($dom->hasChildNodes()) {
writeToString($dom->firstChild, $deep, $text);
}
}
if ($dom->nodeType == TEXT_NODE) {
$text .= "<span style=\"color:#000000\">";
if (trim($dom->getData()) != "") {
$text .= $space;
$data = trim($dom->getData());
$data = str_replace("\t", " ", $data);
while (!(strpos($data, " ") === false)) {
$data = str_replace(" ", " ", $data);
}
$data = htmlspecialchars($data);
$data = str_replace("\n ", "\n", $data);
$data = str_replace("\n", "\n".$space, $data);
$text .= $data;
if (trim($dom->getData()) != "") {
$text .= "<br />";
}
}
$text .= "</span>";
}
if ($dom->nodeType == PROCESSING_INSTRUCTION_NODE) {
$text .= $space;
$text .= "<span style=\"color:#999900\">";
$text .= "<?".$dom->getTarget()." ";
$output = htmlspecialchars($dom->getData());
$output = trim($output);
$output = str_replace(" ", " ", $output);
$output = str_replace("\t", " ", $output);
if (!(strpos($output, "\n")) === false) {
$output = "<br />".$space.$output."<br />".$space;
$output = str_replace("\n", "<br />".$space, $output);
$text .= $output;
$text .= "?><br />";
}
else {
$text .= $output;
$text .= " ?><br />";
}
$text .= "</span>";
}
if (isset($dom->nextSibling)) {
writeToString($dom->nextSibling, $deep, $text);
}
}
function htmlHeader() {
echo '<?xml version="1.0"?>';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '.
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
echo '<head><title>phpXMLDOM: Samples - writer2.php</title></head>';
echo '<body>';
}
function htmlFooter() {
echo '</body>';
echo '</html>';
}
htmlHeader();
$parser = new DOMParser();
$document = $parser->parseFile("../files/sample.xml", true);
write($document);
htmlFooter();
?>
|