• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Is there a better way than DOMDocument?

Jaxel

Well-known member
#1
Right now I am using DOMDocument (as does XenForo) to create XML. Is there a better way?

Just to get this VERY SIMPLE XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
  <url>
    <loc>http://www.8wayrun.com/media/necxi-sc4-ft10-50-mm-mick-vs-dina.1804/</loc>
    <video:video>
      <video:thumbnail_loc>http://www.8wayrun.com/data/media/1804.jpg</video:thumbnail_loc>
      <video:title><![CDATA[NECXI SC4 - FT10 $50 MM - Mick VS Dina]]></video:title>
      <video:description><![CDATA[Streaming video brought to you by Jaxel of http://www.8wayrun.com. This was part of the Soulcalibur IV Money Matches at NEC XI in Philadelphia, PA. Stream can always be found by following Jaxel on twitter: http://twitter.com/JasonAxelrod
Want Jaxel to stream one of your tournaments? Contact him on SRK or 8WR to learn how to hire him. Sponsorship opportunities also available.]]></video:description>
      <video:player_loc><![CDATA[http://www.youtube.com/v/jat8T6x2NxU?autoplay=1&hd=1&fs=1]]></video:player_loc>
      <video:duration>3046</video:duration>
      <video:view_count>26</video:view_count>
      <video:publication_date>2010-12-09T22:09:51+00:00</video:publication_date>
      <video:tag>sophitia</video:tag>
      <video:tag>xianghua</video:tag>
      <video:category><![CDATA[BigE Gaming / GVN]]></video:category>
      <video:uploader info="http://www.8wayrun.com/members/jaxel.1/">Jaxel</video:uploader>
    </video:video>
  </url>
</urlset>
It takes this amazingly large amount of confusing code...
Code:
<?php

class EWRmedio_Model_Sitemaps extends XenForo_Model
{
	public function getMRSSbyMedia($media)
	{
		$document = new DOMDocument('1.0', 'utf-8');
		$document->formatOutput = true;

			$set_node = $document->createElement('urlset');
					$xns_node = $document->createAttribute('xmlns');
					$xns_node->appendChild($document->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9'));
					$vns_node = $document->createAttribute('xmlns:video');
					$vns_node->appendChild($document->createTextNode('http://www.google.com/schemas/sitemap-video/1.1'));

				$set_node->appendChild($xns_node);
				$set_node->appendChild($vns_node);

				$url_node = $document->createElement('url');
					$url_node->appendChild($document->createElement('loc', XenForo_Link::buildPublicLink('full:media', $media)));

					$vid_node = $document->createElement('video:video');
							$tit_node = $document->createElement('video:title');
							$tit_node->appendChild($document->createCDATASection($media['media_title']));
							$des_node = $document->createElement('video:description');
							$des_node->appendChild($document->createCDATASection(XenForo_Helper_String::bbCodeStrip($media['media_description'])));
							$mov_node = $document->createElement('video:player_loc');
							$mov_node->appendChild($document->createCDATASection($media['service_movie']));
							$cat_node = $document->createElement('video:category');
							$cat_node->appendChild($document->createCDATASection($media['category_name']));
							$tags = explode(',', $media['media_keywords']);

						$vid_node->appendChild($document->createElement('video:thumbnail_loc', XenForo_Application::get('options')->boardUrl.'/data/media/'.$media['media_id'].'.jpg'));
						$vid_node->appendChild($tit_node);
						$vid_node->appendChild($des_node);
						$vid_node->appendChild($mov_node);
						$vid_node->appendChild($document->createElement('video:duration', $media['media_duration']));
						$vid_node->appendChild($document->createElement('video:view_count', $media['media_views']));
						$vid_node->appendChild($document->createElement('video:publication_date', date("c", $media['media_date'])));
						foreach ($tags AS $tag) { $vid_node->appendChild($document->createElement('video:tag', trim($tag))); }
						$vid_node->appendChild($cat_node);

						$usr_node = $document->createElement('video:uploader', $media['username']);
								$inf_node = $document->createAttribute('info');
								$inf_node->appendChild($document->createTextNode(XenForo_Link::buildPublicLink('full:members', $media)));
							$usr_node->appendChild($inf_node);

					$vid_node->appendChild($usr_node);
				$url_node->appendChild($vid_node);
			$set_node->appendChild($url_node);
		$document->appendChild($set_node);

		return $document;
	}
}
There has to be a better way...
 

Jaxel

Well-known member
#4
Okay... I simplified my code a bit by nesting my node generation and replacing CDATA generation with simple htmlspecialchars.

Code:
<?php

class EWRmedio_Model_Sitemaps extends XenForo_Model
{
	public function getMRSSbyMedia($media)
	{
		$document = new DOMDocument('1.0', 'utf-8');
		$document->formatOutput = true;

		$document->appendChild($rss_node = $document->createElement('rss'));
			$rss_node->appendChild($ver_node = $document->createAttribute('version'));
				$ver_node->appendChild($document->createTextNode('2.0'));
			$rss_node->appendChild($med_node = $document->createAttribute('xmlns:media'));
				$med_node->appendChild($document->createTextNode('http://search.yahoo.com/mrss/'));

			$rss_node->appendChild($chn_node = $document->createElement('channel'));
				$chn_node->appendChild($document->createElement('title', XenForo_Application::get('options')->boardTitle));
				$chn_node->appendChild($document->createElement('link', XenForo_Link::buildPublicLink('full:media')));
				$chn_node->appendChild($document->createElement('description', XenForo_Application::get('options')->boardDescription));
				$chn_node->appendChild($this->buildMRSSbyMedia($document, $media));

		return $document;
	}

	public function buildMRSSbyMedia($document, $media)
	{
		$itm_node = $document->createElement('item');
			$itm_node->appendChild($document->createElement('pubDate', date("r", $media['media_date'])));
			$itm_node->appendChild($document->createElement('title', htmlspecialchars($media['media_title'])));
			$itm_node->appendChild($document->createElement('link', XenForo_Link::buildPublicLink('full:media', $media)));
			$itm_node->appendChild($document->createElement('description', htmlspecialchars(XenForo_Helper_String::bbCodeStrip(str_replace("\n", " ", $media['media_description'])))));
			$itm_node->appendChild($document->createElement('media:category', htmlspecialchars($media['category_name'])));
			$itm_node->appendChild($document->createElement('media:keywords', htmlspecialchars($media['media_keywords'])));
			$itm_node->appendChild($ply_node = $document->createElement('media:player'));
				$ply_node->appendChild($url_node = $document->createAttribute('url'));
					$url_node->appendChild($document->createTextNode($media['service_movie']));
			$itm_node->appendChild($thu_node = $document->createElement('media:thumbnail'));
				$thu_node->appendChild($url_node = $document->createAttribute('url'));
					$url_node->appendChild($document->createTextNode(XenForo_Application::get('options')->boardUrl.'/data/media/'.$media['media_id'].'.jpg'));
			$itm_node->appendChild($cnt_node = $document->createElement('media:content'));
				$cnt_node->appendChild($med_node = $document->createAttribute('medium'));
				$cnt_node->appendChild($dur_node = $document->createAttribute('duration'));
					$med_node->appendChild($document->createTextNode('video'));
					$dur_node->appendChild($document->createTextNode($media['media_duration']));

		return $itm_node;
	}
}
My question now... is this right?

I have a feed here: http://www.8wayrun.com/media/wgc-2k10-sc4-maxou-vs-thugish-pond-part-3.1868/rss
Here is the recent feed: http://www.8wayrun.com/media/rss

However, when viewing this feed in Chome, IE and FF, I dont see the thumbnail... What did I do wrong?

Code:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>8WayRun.Com</title>
    <link>http://www.8wayrun.com/media/</link>
    <description>A Tournament Competitive Community for the Soulcalibur series</description>
    <item>
      <pubDate>Sun, 12 Dec 2010 18:26:30 +0000</pubDate>
      <title>WGC -2K10 - SC4 - Maxou vs Thugish Pond Part 3</title>
      <link>http://www.8wayrun.com/media/wgc-2k10-sc4-maxou-vs-thugish-pond-part-3.1868/</link>
      <description>Match grande finale du tournoi main event soulcalibur 4 solo lors du world game cup 2010  Commentateur: Asenka</description>
      <media:category>Soulcalibur France</media:category>
      <media:keywords>amy, setsuka</media:keywords>
      <media:player url="http://www.youtube.com/v/5WiRTR6TuE4?autoplay=1&amp;hd=1&amp;fs=1"/>
      <media:thumbnail url="http://www.8wayrun.com/data/media/1868.jpg"/>
      <media:content medium="video" duration="361"/>
    </item>
  </channel>
</rss>