XML

Creating a simple news script that reads from an XML file

I recently recoded a website for a friend because his markup was pretty ugly (thanks to WYSIWYG editors). While I was doing that, he mentioned he’d like to have an xml file he could edit to update his news. Simple enough I said and banged out the following which includes a little helpful function that wraps paragraphs in HTML p tags. My friend alpha helped me rewrite some ugly regex with a novel idea:

print join '', map {qq[

$_

]} split (/\r?\n/,$object->content)

Of course, he’s a big perl coder so I had to create a php equivalent, nls2p.

Edit: Mookow mentioned he wanted to write links, so obviously a system would have to be put in place to support this. Using HTML tags in XML gets a little sloppy so I opted to use bbcode which is a very popular method to format text in web applications and very simple to add more filters.

The news post creator:

<?php

if (file_exists('news.xml')) {
    $xml = simplexml_load_file('news.xml');
	foreach ($xml->post as $post) {
	   echo "
<h4>".$post->title." - ".$post->date."</h4>

\n";
	   echo "
<div>".bbcode(nls2p($post->content))."</div>

\n";
	   echo "

--".$post->author."

\n";
	}
} else {
    echo "

Failed to open news source

";
}

The functions for formatting:

// Wrap paragraphs with

 tags
function nls2p($str) {
	$split_str = preg_split('#[\t]+?[\r\n]{1}#',$str);
	foreach ($split_str as $line) {
		$new_str = $new_str."

".preg_replace("/[\t\n]/","",$line)."

\n";
	}
	return $new_str;
}

// Parse bbcode as HTML
function bbcode($string) {
	$search = array(
		'@\[(?i)b\](.*?)\[/(?i)b\]@si',
		'@\[(?i)i\](.*?)\[/(?i)i\]@si',
		'@\[(?i)u\](.*?)\[/(?i)u\]@si',
		'@\[(?i)img\](.*?)\[/(?i)img\]@si',
		'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
		'@\[(?i)code\](.*?)\[/(?i)code\]@si'
	);
	$replace = array(
		'<b>\\1</b>',
		'<i>\\1</i>',
		'<u>\\1</u>',
		'<img src="\\1">',
		'<a href="\\1" target="_blank">\\2</a>',
		'<code>\\1</code>'
	);
	return preg_replace($search , $replace, $string);
}
?>

The XML data file:

<?xml version='1.0' standalone='yes'?>
<news>
<post>

		<date>03-29-09</date>
		<author>Moo</author>
		<content>The site has been completely recoded by a friend of mine, -z- from NexuizNinjaz.com. He even added this fancy news script. Now, to update the news, I don't even have to touch index.php! Alright, I'm out for now.

		OMG A [b]NEW LINE[/b]!

		Now with [i]italics[/i] and [u]underlines[/u] and [url=http://www.nexuizninjaz.com]links[/url]

		[code]code blocks[/code]

		and images

		[img]http://www.xepic.net/pics/pic_091622001182920068.jpg[/img]
		</content>
	</post>
<post>

		<date>03-29-09</date>
		<author>Moo</author>
		<content>Yeah, I removed the stupid gray background, because after a while it got boring. I changed the "Random Shiznit" column into something that could be a bit more productive. Once all of the other sites are up, I'll update the main page with the latest news from those. This main news column will be for the more intriguing news. I also decided to add titles to the news articles...Duuhhhhhhh

		OMG A NEW LINE!

		and again, LOL!
		</content>
	</post>
<post>

		<date>03-25-09</date>
		<author>Moo</author>
		<content>I added a boring gray background to the main page, as you can see. The hover text-changing effect was removed because of its stupidity. A cool guy named Fabzor re-colored the header for me.</content>
	</post>
<post>

		<date>03-23-09</date>
		<author>Moo</author>
		<content>Website updated, not much. Cool hover effects were added, but we still don't have much color in the page. Hopefully, I can find a good background and get this show on the road.</content>
	</post>
<post>

		<date>03-22-09</date>
		<author>Moo</author>
		<content>Most of the links up top will not work for the time being, this site is just getting edited. Soon, we'll have a better webpage than this one, but for now, deal with this one!</content>
	</post>
</news>

That’s pretty much it verbatim, including mookow’s news ^_^. Hope this is helpful to someone.

NO to the Microsoft Office format as an ISO standard

I ask the national members of ISO to vote “NO” in the ballot of ISO DIS 29500 (Office OpenXML or OOXML format) for the following reasons:

1. There is already a standard ISO26300 named Open Document Format (ODF): a dual standard adds costs, uncertainty and confusion to industry, government and citizens;
2. There is no provable implementation of the OOXML specification: Microsoft Office 2007 produces a special version of OOXML, not a file format which complies with the OOXML specification;
3. There is information missing from the specification document, for example how to do a autoSpaceLikeWord95 or useWord97LineBreakRules;
4. More than 10% of the examples mentioned in the proposed standard do not validate as XML;
5. There is no guarantee that anybody can write software that fully or partially implements the OOXML specification without being liable to patent lawsuits or patent license fees by Microsoft;
6. This format conflicts with existing ISO standards, such as ISO 8601 (Representation of dates and times), ISO 639 (Codes for the Representation of Names and Languages) or ISO/IEC 10118-3 (cryptographic hash);
7. There is a bug in the spreadsheet file format which forbids any date before the year 1900: such bugs affect the OOXML specification as well as software applications like Microsoft Excel 2000, XP, 2003 and 2007.
8. This standard proposal was not created by bringing together the experience and expertise of all interested parties (such as the producers, sellers, buyers, users and regulators), but by Microsoft alone.

Sign the petition here