[Agavi-Users] Steps to output XML

102 views
Skip to first unread message

vikram.m...@googlemail.com

unread,
Dec 26, 2008, 12:06:35 AM12/26/08
to us...@lists.agavi.org
Hi all

Merry Christmas :)

I have an Index action/view which lists all the records in my
database. This takes an additional 'page' parameter so that the
records appear in pages.

I'm trying now to generate an XML version of this. The XML version
will include all the records. ie. no paging is needed.

I read some discussions on this list and realized I don't need a new
action - I can just use the same one, and create an executeXML method
in my view. I did this, also set up a new output type as follows:

In output_types.xml
<output_type name="xml">
<parameter name="http_headers">
<parameter name="Content-Type">text/xml</parameter>
</parameter>
</output_type>

In routing.xml:
<route name="author.index.xml" pattern="^/author/index/xml$"
action="Author.Index" output_type="xml" />


In the view:
public function executeXml(AgaviRequestDataHolder $rd)
{
$c = new Criteria();
$c->addAscendingOrderByColumn(AuthorPeer::AUTHORID);

$authors = AuthorPeer::doSelect($c);
$this->setAttribute('dataset', $authors);
}

In the template Index.xml.php:

<?xml version='1.0'?>
<?php if (isset($template['dataset']) && count($template['dataset']) > 0): ?>
<div>
<?php foreach ($template['dataset'] as $d): ?>
<div><?php echo $d->getAuthorName(); ?></div>
<?php endforeach; ?>
</div>
<?php endif; ?>

but the eventual output does not contain any XML: I get a blank page.
Can anyone help? I'm sure I'm doing more than one thing wrong :)

Vikram

_______________________________________________
users mailing list
us...@lists.agavi.org
http://lists.agavi.org/mailman/listinfo/users

Felix Gilcher

unread,
Dec 26, 2008, 5:17:08 AM12/26/08
to vikram.m...@gmail.com, Agavi Users Mailing List
Merry Christmas,

in general your code should work, but the template gets never called
as your output type has no renderer assigned. However, generating XML
using string manipulation is probably not the best way to go - using
DOMXml or SimpleXML will probably use more memory but will at least
make sure that the XML is wellformed.

So there are some minor things to improve:

- I'd assign a global route for XML output:

<route name="xml_output" pattern=".xml$" cut="true" stop="false"
output_type="xml" /> but that's a bit a matter of taste. However, if
you choose not to do that, you can at least nest your author routes:

<route name="/author" pattern="author" action="Author">
<route name=".xml" pattern="/xml$" cut="true" output_type="xml" />
<route name=".index" pattern="/index$" action=".Index" />
</route>

Without a renderer, the view will not load a template but will output
what you return from the executeXml function:

public function executeXml(AgaviRequestDataHolder $rd)
{
$c = new Criteria();
$c->addAscendingOrderByColumn(AuthorPeer::AUTHORID);

$authors = AuthorPeer::doSelect($c);

$doc = new DOMDocument('1.0', 'utf-8');

$root = $doc->appendchild($doc->createElement('authors'));

foreach ($authors as $author)
{
$root->appendChild($doc->createElement('author', $d-
>getAuthorName()));
}

return $doc->saveXml();
}

(all untested code from the top of my head, but you'll get the idea)

cheers

felix

--
Felix Gilcher

Bitextender GmbH
Paul-Heyse-Str. 6
D-80336 München

T: +49 89 57 08 15 16
F: +49 89 57 08 15 17
M: +49 172 840 88 28

felix....@bitextender.com
http://bitextender.com/

Amtsgericht München, HRB 174280
Geschäftsführer: David Zülke, Florian Clever

Reply all
Reply to author
Forward
0 new messages