XMLWriter or Strings?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DigiBrisk
Forum Newbie
Posts: 2
Joined: Sun Aug 19, 2007 11:49 pm

XMLWriter or Strings?

Post by DigiBrisk »

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:

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);

?>
rather than this:

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;

?>
to make one of these:

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>
(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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The advantages of such classes usually are: syntactically correct documents, including entity translation and attribute/namespace handling.
It's a pitty that the methods of php5's XmlWriter do not return the object itself. That would make chained nmethod calls possible, like e.g.

Code: Select all

$xmlwriter
  ->startElementNS('foo', 'bar', 'http://foo.bar/');
    ->writeElement('xyz', '1<2');
  ->endElement();
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

volka wrote:

Code: Select all

$xmlwriter
  ->startElementNS('foo', 'bar', 'http://foo.bar/');
    ->writeElement('xyz', '1<2');
  ->endElement();
Minus the semicolons...?


I think the only advantage of a class over actually writing the document is that you are less likely to make a silly error in the markup. It's pretty big advantage when you consider how easy it is to slip up, however.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

superdezign wrote:Minus the semicolons...?
yes.
DigiBrisk
Forum Newbie
Posts: 2
Joined: Sun Aug 19, 2007 11:49 pm

Post by DigiBrisk »

Mmm. I see the idea behind that, now. I can see the advantage with, say, really large projects especially. (Come to think of it, though, bigname projects like MediaWiki don't use XMLWriter.)

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Another distinction is that both of your examples are done with code. You could also go the template route:

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>{$error_message}</p>
      
      <form method="post" action="{$action_url}">
         <p>Submit a line here:<br />
         <input type="text" name="form_line" /></p>
         
         <p><input type="submit" name="form_submit" /></p>
      </form>
   </body>
</html>
The above can be edited by a non-programmer and validated with many programs -- such as browsers.
(#10850)
Post Reply