Hi there,

I've set up a Drupal website to output a View as XML which is to be used with a Flash slideshow. I have done so using various functions / suggestions I've found throughout the forums.

The XML can be seen here:
http://dev.goldcoastholidaypark.com.au/xml/12/slideshow.xml

The issue I've got is that Flash can't interpret the XML because of the "<div class="view view-gallery-xml">" and "<div class="view-content view-content-gallery-xml">" tags and their closing tags.

I can't for the life of me work out how to ensure that those tags are not output in the view?

Here is the code in my template.php file that is being used...

/** Custom Gallery XML View **/
function phptemplate_views_view_list_gallery_xml($view, $nodes, $type) {
  $output ="";
  
  foreach($nodes as $n) {
    $output .= "<image img=\"/files/thumbnails/".$n->node_data_field_thumbnail_field_thumbnail_alt."\" />";
  }
  return $output;
}

As to ensure that the view is output as XML, I have a file called "page-xml.tpl.php" that is structured as follows...

<?php
drupal_set_header('Content-Type: text/xml; charset=utf-8');
print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
print "<slideshow>";
print $content;
print "</slideshow>";
?>

There is a bit more stuff in that file, but you get the idea.

Any suggestions? I'm guessing it's probably something straight forward that I'm overlooking.

Comments

emackn’s picture

I believe those div tags are coming from the default theming on view list items.

Have you tried using the complete override for all gallery_xml views?
phptemplate_views_view_gallery_xml($view, $type, $nodes, $level = NULL, $args = NULL) {...}
instead of
phptemplate_views_view_list_gallery_xml($view, $nodes, $type) {...}

Or is that not an option?

deanx0r’s picture

You are awesome! I thought I'd tried that but obviously not, just that simple change worked a charm!

I was halfway through putting some if statements in the views.module file to ensure they wouldn't be output, but that's a MUCH better solution!

Thanks heaps for your help. :)

mediashock’s picture

I am tryin to replicate this also.

i put

function  phptemplate_views_view_gallery_xml($view, $type, $nodes, $level = NULL, $args = NULL) {
  $output ="";
 
  foreach($nodes as $n) {
    $output .= "<image img=\"/files/thumbnails/".$n->node_data_field_thumbnail_field_thumbnail_alt."\" />";
  }
  return $output;
}

into template.php

and created page-xml.tpl.php with the other code.. how do i call it though.. sorry i am pretty new to drupal still

headkit’s picture

why is your page called "page-xml.tpl.php"?

headkit’s picture

is there a tutorial anywhere to find...?
i really need well formed xml output!
thnx.

emackn’s picture

headkit’s picture

thank you, i will try that again.
i bet this will work now.

the funny thing is that the link leads back to this thread, if you follow the trace...
:-)

macdonaldj’s picture

I was in need to generate an xml file for use in a flash photo gallery. I couldn't find one way to do it, so I came up with this way. If there is a better/easier method, please let me know.

the xml needed to be in this format:

  <galleries>
    <gallery title="first album">
        <image caption="a caption" full="some_url/pic1.jpg" thumb="some_url/pic1_thumb.jpg" />
        <image caption="a 2 caption" full="some_url/pic2.jpg" thumb="some_url/pic2_thumb.jpg" />
    </gallery>
    <gallery title="2nd album">
        <image caption="a caption" full="some_url/pic3.jpg" thumb="some_url/pic3_thumb.jpg" />
        <image caption="a 2 caption" full="some_url/pic4.jpg" thumb="some_url/pic4_thumb.jpg" />
    </gallery>
  </galleries>

Each photo album/gallery was coming from a content type of "gallery" that had an image field that let users upload multiple images per album. The thumb for each was created by imagecache using "scale and crop" action.

I created a new view for the node type of gallery. I used row style of node instead of fields. Changed the path to "gallery_xml/gallery.xml" .

I overrode these theme files for the view:

display output: views-view--gallery.tpl.php - to only show the rows

    <?php print $rows; ?>

style output: views-view-unformatted--gallery.tpl.php - to remove and div styles

    <?php foreach ($rows as $id => $row): ?>
      <?php print $row; ?>  
    <?php endforeach; ?>

By Using the fields as nodes, I can theme the node file to output the gallery xml

node-view-gallery.tpl.php

  <?php
	global $base_url;
	print "<GALLERY TITLE=\"".$node->title."\">";
	foreach ($node->field_album_image as $image)
	{		
		print "<IMAGE CAPTION=\"".$image['data']['description']."\" FULL=\"".$base_url."/".$image['filepath']."\" THUMB=\"".$base_url."/sites/default/files/imagecache/gallery_thumb/gallery/".$image['filename']."\" />";		
	}
	print "</GALLERY>";
   ?>

Now I needed to get the GALLERIES root tag in the xml file.

I created a page-gallery_xml.tpl.php

  <?php
    drupal_set_header('Content-Type: text/xml; charset=utf-8');
    print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    print "<GALLERIES> ";
    print $content;
    print "</GALLERIES >";

  ?>

Now by going to gallery_xml/gallery.xml, I got the correct xml layout. I changed the flash to grab the xml from this location, and all worked.

Hopefully this will help someone.

mike27’s picture

Thank you macdonaldj,
I am trying to make something similar (http://drupal.org/node/838938).

I followed your instructions but i can not set the page header to xml..
The source output is

GALLERIES> GALLERY TITLE =".." > /GALLERY>
GALLERY TITLE=".."> /GALLERY>
GALLERY TITLE=".."> /GALLERY>
GALLERY TITLE=".."> /GALLERY>

but in the display i have the following error

XML declaration allowed only at the start of the document

Please help.

Thank you anyway, again,

mike

mike27’s picture

the function

print ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

leaves a blank space in the beggining

output:
<?xml version="1.0" encoding="utf-8"?>
instead of
<?xml version="1.0" encoding="utf-8"?>

This caused the problem.

The solution is to replace
print ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
with
$xml = new DomDocument('1.0','utf-8');

Still further help appreciated:)
mike

superfluousapostrophe’s picture

I was following these steps and couldn't get it to work. I figured it out after a day or so of messing around. In your view, in the information link, be sure to click "Rescan template files" after you've added the new template files or else Drupal won't recognize your files.

Thanks for this, it solved my XML output problems :)