组合 XmlWriter 对象?
我的应用程序的结构方式是,每个组件都会生成 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。它更灵活,而且似乎表现更好(至少在我创建的粗略测试中)。
The way my application is structured, each component generates output as XML and returns an XmlWriter object. Before rendering the final output to the page, I combine all XML and perform an XSL transformation on that object. Below, is a simplified code sample of the application structure.
Does it make sense to combine XmlWriter objects like this? Is there a better way to structure my application? The optimal solution would be one where I didn't have to pass a single XmlWriter instance as a parameter to each component.
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);
Update:
I may abandon XmlWriter altogether and use DomDocument instead. It is more flexible and it also seemed to perform better (at least on the crude tests I created).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这个架构中,我宁愿将 Writer 的集合传递给输出,沿着以下路线
in this architecture i'd rather pass a collection of Writers to output, along the lines of
我从未真正见过有人以这种方式组合 XmlWriter 对象,而且我认为这对于我想做的事情来说不是很有效。我决定最好的方法是使用 DOMDocument。区别在于:DOMDocument 在输出之前不会生成任何 XML,而 XmlWriter 基本上是一个 StringBuilder,不太灵活。
I've never actually seen anyone combine XmlWriter objects this way and I don't think it is very efficient for what I am trying to do. I decided the best approach would be to use DOMDocument instead. The difference is: DOMDocument does not generate any XML until you output, whereas XmlWriter is basically a StringBuilder and is not as flexible.
我会让 page1Xml 和generateSiteMap 获取编写器作为输入,并将其作为输出返回
I would have page1Xml and generateSiteMap get a writer as input, and return it as output