XMLWriter or Strings?
Posted: Mon Aug 20, 2007 2:00 am
For those who may know: Are there any real advantages to using the XMLWriter class for generating XML (specifically XHTML) rather than using strings? That's to say, I'd work with this:
rather than this:
to make one of these:
(The code was quickly hacked up; it wasn't even tested or anything. No need to read into it too much.)
Are there any advantages to using XMLWriter to output my XHTML documents, a la the first method, or would it be a lot easier to just use strings, a la the second method?
Code: Select all
<?php
// XHTML output test
// Using XMLWriter class
// 1. Initialize XMLWriter class.
$xmlwriter = new XMLWriter();
// 2. Tell class to open a memory block for writing. Also set parameters here.
$xmlwriter->openMemory();
$xmlwriter->setIndentString("\t");
$xmlwriter->setIndent(true);
// 3. Write XHTML 1.0 Strict DTD.
$xmlwriter->startDtd("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
$xmlwriter->endDtd();
// 4. Start HTML document.
$xmlwriter->startElement("html");
$xmlwriter->writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
$xmlwriter->writeAttribute("xml:lang", "en");
$xmlwriter->writeAttribute("lang", "en");
// <head>
$xmlwriter->startElement("head");
// a. Was form input detected? Note so here.
if(!empty($_POST["form_line"])) { $xmlwriter->writeElement("title", "HTML Form Title - Form Input Detected!"); }
else { $xmlwriter->writeElement("title", "HTML Form Title"); }
$xmlwriter->endElement();
// <body>
$xmlwriter->startElement("body");
$xmlwriter->writeElement("h1", "HTML Form Header");
// b. Was form input detected? Note so here.
if(!empty($_POST["form_line"])) {
$xmlwriter->writeElement("p", "Form input detected! Line submitted: \"".$_POST["form_line"]."\"");
}
// Start writing the <form> block here.
$xmlwriter->startElement("form");
$xmlwriter->writeAttribute("method", "post");
$xmlwriter->writeAttribute("action", $_SERVER["PHP_SELF"]);
$xmlwriter->startElement("p");
$xmlwriter->text("Submit a line here:");
$xmlwriter->writeElement("br");
$xmlwriter->startElement("input");
$xmlwriter->writeAttribute("type", "text");
$xmlwriter->writeAttribute("name", "form_line");
$xmlwriter->endElement();
$xmlwriter->endElement();
$xmlwriter->startElement("p");
$xmlwriter->startElement("input");
$xmlwriter->writeAttribute("type", "submit");
$xmlwriter->writeAttribute("name", "form_submit");
$xmlwriter->endElement();
$xmlwriter->endElement();
$xmlwriter->endElement();
$xmlwriter->endElement();
$xmlwriter->endElement();
// 5. Done writing HTML document! Output the data and call it a day.
echo $xmlwriter->outputMemory(true);
?>Code: Select all
<?php
// XHTML output test
// Using String manipulation
// 1. Initialize $final_html. We'll store our HTML code here.
$final_html = "";
// 2. Start writing HTML.
$final_html .= "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>" . "\r\n";
$final_html .= "\r\n";
$final_html .= "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>" . "\r\n";
// <head>
$final_html .= "\t"."<head>" . "\r\n";
// 3. Was form input detected? Note so here.
if(!empty($_POST["form_line"])) {
$final_html .= "\t\t"."<title>HTML Form Title - Form Input Detected!</title>" . "\r\n";
} else {
$final_html .= "\t\t"."<title>HTML Form Title</title>" . "\r\n";
}
$final_html .= "\t"."</head>" . "\r\n";
// <body>
$final_html .= "\t"."<body>" . "\r\n";
$final_html .= "\t\t"."<h1>HTML Form Header</h1>" . "\r\n";
// 4. Was form input detected? Note so here.
if(!empty($_POST["form_line"])) {
$final_html .= "\t\t"."<p>Form input detected! Line submitted: '".$_POST["form_line"]."'</p>" . "\r\n";
}
$final_html .= "\r\n";
// Start writing the <form> block here.
$final_html .= "\t\t"."<form method='post' action='$_SERVER['PHP_SELF']'>" . "\r\n";
$final_html .= "\t\t\t"."<p>Submit a line here:<br />" . "\r\n";
$final_html .= "\t\t\t"."<input type='text' name='form_line' /></p>" . "\r\n";
$final_html .= "\r\n";
$final_html .= "\t\t\t"."<p><input type='submit' name='form_submit' /></p>" . "\r\n";
$final_html .= "\t\t"."</form>";
$final_html .= "\t"."</body>";
$final_html .= "</html>";
// 5. Done writing the HTML document! Echo the string in its entirety and call it a day.
echo $final_html;
?>Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>HTML Form Title - Form Input Detected!</title>
</head>
<body>
<h1>HTML Form Header</h1>
<p>Form input detected! Line submitted: "Test"</p>
<form method="post" action="self.html">
<p>Submit a line here:<br />
<input type="text" name="form_line" /></p>
<p><input type="submit" name="form_submit" /></p>
</form>
</body>
</html>Are there any advantages to using XMLWriter to output my XHTML documents, a la the first method, or would it be a lot easier to just use strings, a la the second method?