xpages – 使用SSJS创建xml文件

我想在服务器上使用SSJS创建一个xml文件.有办法吗?任何人都可以提供一个示例代码来在服务器上创建一个xml文件.

解决方法

有很多方法.看似最简单的是创建一个看起来像 XML的字符串.

下一个是使用Java DOM类.有an article描述它.

最后你可以使用SAX和一个小helper class

让我们知道怎么回事.

更新:这将是我的@ Michael代码示例版本:

<?xml version="1.0" encoding="UTF-8"?>
<!-- XPage which is not rendered but returns data like XML,JSON,etc.     -->
<!-- More: http://www.wissel.net/blog/d6plinks/shwl-7mgfbn                 -->

<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
    <xp:this.beforeRenderResponse><![CDATA[#{javascript:try {
    var out = facesContext.getOutputStream();
    var exCon = facesContext.getExternalContext();
    var response = exCon.getResponse(); // get the response context
    // set content type,e.g. ...
    response.setContentType("text/xml"); 
    // set caching option 
    response.setHeader("Cache-Control","no-cache");
    // write XML output ...
    var result = new biz.taoconsulting.xmltools.SimpleXMLDoc();
    result.setOut(out);
    result.openTag("result");
    result.dateTag("created",new java.util.Date());
    result.addSimpleTag("Author",@UserName);
    result.openTag("FruitList");
    result.addComment("Stephan really likes the fruits example");
    var attributes = new java.util.HashMap();
    attributes.add("name","Durian");
    attributes.add("color","white");
    attributes.add("taste","Don't ask");
    result.addEmptyTag("fruit",attributes);
    result.closeDocument();
    // close the output
    exCon.responseComplete();
    out.close();
} catch (e) {
    print(e.toString());
}}]]>
    </xp:this.beforeRenderResponse>
</xp:view>

请注意这里的差异:

>我使用beforeRenderResponse事件
>访问outputStream而不是writer(在afterRenderResponse事件中无法访问流)
>设置响应完成以阻止页面进一步输出,因此您只需在页面上键入注释即可
>使用助手类

当您阅读辅助类的源代码时,有点奇怪:为什么不在构造函数中使用输出流,所以您不会错过它? – 我今天会添加第二个构造函数,但无参数构造函数允许我将该类定义为托管bean,如果我喜欢它.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念