按照我的应用程序的结构方式,每个组件都以XML的形式生成输出并返回一个XmlWriter对象。在将最终输出呈现给页面之前,我组合所有XML并对该对象执行XSL转换。下面是应用程序结构的简化代码示例。
像这样组合XmlWriter对象有意义吗?是否有更好的方法来构造我的应用程序?最优的解决方案是,我不必将单个XmlWriter实例作为参数传递给每个组件。
function page1Xml() {
$content = new XmlWriter();
$content->openMemory();
$content->startElement('content');
$content->text('Sample content');
$content->endElement();
return $content;
}
function generateSiteMap() {
$sitemap = new XmlWriter();
$sitemap->openMemory();
$sitemap->startElement('sitemap');
$sitemap->startElement('page');
$sitemap->writeAttribute('href', 'page1.php');
$sitemap->text('Page 1');
$sitemap->endElement();
$sitemap->endElement();
return $sitemap;
}
function output($content)
{
$doc = new XmlWriter();
$doc->openMemory();
$doc->writePi('xml-stylesheet', 'type="text/xsl" href="template.xsl"');
$doc->startElement('document');
$doc->writeRaw( generateSiteMap()->outputMemory() );
$doc->writeRaw( $content->outputMemory() );
$doc->endElement();
$doc->endDocument();
$output = xslTransform($doc);
return $output;
}
$content = page1Xml();
echo output($content);更新:
我可以完全放弃XmlWriter,转而使用DomDocument。它更灵活,而且似乎表现得更好(至少在我创建的粗糙测试中是如此)。
发布于 2009-12-03 05:09:00
我从来没有见过有人以这种方式组合XmlWriter对象,我不认为它对我想要做的事情非常有效。我决定最好的方法是使用DOMDocument。区别在于: DOMDocument在输出之前不会生成任何XML,而XmlWriter基本上是一个StringBuilder,没有那么灵活。
发布于 2009-11-30 00:23:50
在这个体系结构中,我宁愿传递一个作家的集合来输出,按照
function output($ary) {
.....
foreach($ary as $w) $doc->writeRaw($w->outputMemory());
.....
}
output(array(page1(), siteMap(), whateverElse()))发布于 2010-06-16 12:46:18
我希望page1Xml和generateSiteMap获得一个写入器作为输入,并将它作为输出返回
https://stackoverflow.com/questions/1817151
复制相似问题