组合 XmlWriter 对象?

发布于 2024-08-13 09:23:16 字数 1304 浏览 8 评论 0原文

我的应用程序的结构方式是,每个组件都会生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯下孤影 2024-08-20 09:23:16

在这个架构中,我宁愿将 Writer 的集合传递给输出,沿着以下路线

 function output($ary) {
     .....
     foreach($ary as $w) $doc->writeRaw($w->outputMemory());
     .....
 }

 output(array(page1(), siteMap(), whateverElse()))

in this architecture i'd rather pass a collection of Writers to output, along the lines of

 function output($ary) {
     .....
     foreach($ary as $w) $doc->writeRaw($w->outputMemory());
     .....
 }

 output(array(page1(), siteMap(), whateverElse()))
浊酒尽余欢 2024-08-20 09:23:16

我从未真正见过有人以这种方式组合 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.

鯉魚旗 2024-08-20 09:23:16

我会让 page1Xml 和generateSiteMap 获取编写器作为输入,并将其作为输出返回

I would have page1Xml and generateSiteMap get a writer as input, and return it as output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文