[Phpxd-commits] CVS: phpXD/samples/writer2 writer2.php,NONE,1.1
Status: Beta
Brought to you by:
growbal
|
From: Thomas D. <th...@us...> - 2002-02-13 21:12:38
|
Update of /cvsroot/phpxd/phpXD/samples/writer2
In directory usw-pr-cvs1:/tmp/cvs-serv2640/samples/writer2
Added Files:
writer2.php
Log Message:
The beginning of more samples... :=)
--- NEW FILE: writer2.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: writer2.php,v 1.1 2002/02/13 21:12:26 thomi Exp $
/**
* Sample PHP script for using phpXMLDOM. Loads the file ../files/sample.xml
* and prints it as XML.
*
* @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 writeToFile(&$document, $file, $trim = false) {
$fp = fopen($file, "w");
fwrite($fp, "<?xml version=\"1.0\"?>\n");
writeToString($document, $trim, $text);
fwrite($fp, $text);
fclose($fp);
}
function writeToString(&$dom, $trim, &$text) {
if ($dom->nodeType == CDATA_SECTION_NODE) {
$text .= "<![CDATA[";
$text .= $dom->getData();
$text .= "]]>";
}
if ($dom->nodeType == COMMENT_NODE) {
$text .= "<!--";
$text .= $dom->getData();
$text .= "-->";
}
if ($dom->nodeType == DOCUMENT_NODE) {
$text .= "<?xml version=\"1.0\"?>\n";
if ($dom->hasChildNodes()) {
writeToString($dom->firstChild, $trim, $text);
}
}
if ($dom->nodeType == DOCUMENT_FRAGMENT_NODE) {
writeToString($dom->firstChild, $trim, $text);
}
if ($dom->nodeType == ELEMENT_NODE) {
$text .= "<".$dom->tagName;
if (isset($dom->attributes)) {
for ($i = 0; $i < $dom->attributes->getLength(); $i++) {
$elem =& $dom->attributes->item($i);
if ($elem->specified) {
$text .= " ".$elem->getName()."=\"".$elem->getValue()."\"";
}
}
}
if ($dom->hasChildNodes()) {
$text .= ">";
writeToString($dom->firstChild, $trim, $text);
$text .= "</".$dom->tagName.">";
}
else {
$text .= " />";
}
}
if ($dom->nodeType == ENTITY_REFERENCE_NODE) {
if ($dom->hasChildNodes()) {
writeToStrring($dom->firstChild, $text, $trim);
}
}
if ($dom->nodeType == PROCESSING_INSTRUCTION_NODE) {
$output = $dom->getData();
if ($trim) {
$output = str_replace("\t", " ", $output);
$output = preg_replace('= =', ' ', $output);
$output = str_replace("\n ", "\n", $output);
}
$text .= "<?".$dom->getTarget()." ";
$text .= $output;
$text .= "?>";
}
if ($dom->nodeType == TEXT_NODE) {
if ($trim) {
$output = $dom->getData();
$output = str_replace("\t", " ", $output);
$output = preg_replace('= =', ' ', $output);
$output = str_replace("\n ", "\n", $output);
$text .= $output;
}
else {
$text .= $dom->getData();
}
}
if (isset($dom->nextSibling)) {
writeToString($dom->nextSibling, $trim, $text);
}
}
$parser = new DOMParser();
$document = $parser->parseFile("../files/sample.xml");
header("Content-Type: text/xml");
write($document, true);
?>
|