【问题标题】:CQ5.6.1 - Error while reading xml file in CQ from servlet in OSGi bundleCQ5.6.1 - 从 OSGi 包中的 servlet 读取 CQ 中的 xml 文件时出错
【发布时间】:2015-01-16 06:09:56
【问题描述】:

我正在使用 Windows 8 中的 CQ5.6.1。我正在使用部署在 OSGi 包中的 servlet。从 servlet,我试图打开一个 xml 文件,该文件存储在 CQ 的路径 /etc/clientlibs/geometrixx 中。这是我用来阅读的代码。

import javax.xml.parsers.DocumentBuilderFactory;
...

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document amSetupDoc = null ;
amSetupDoc = factory.newDocumentBuilder().parse(new File("/etc/clientlibs/geometrixx/am/BaseAMStock_Settings.xml"));

此时,我收到以下异常。

java.io.FileNotFoundException: D:\etc\clientlibs\geometrixx\am\BaseAMStock_Settings.xml (The system cannot find the path specified)

我不知道为什么路径被转换为 Windows 路径。有没有更好的方法从我的 servlet 读取 CQ 存储库中的文件?我很感激你能给我的任何建议。谢谢。

【问题讨论】:

标签: xml servlets aem filenotfoundexception osgi-bundle


【解决方案1】:

当我在处理类似的功能时,我使用了javax.jcr.Sessionjavax.jcr.Node。此外,请注意,您的所有数据并未存储在/etc/clientlibs/geometrixx/am/BaseAMStock_Settings.xml 中,而是存储在名为jcr:content 的子节点的属性jcr:data 中。

您可以使用 CRXDE 轻松检查:http://localhost:4502/crx/de/index.jsp#/etc/clientlibs/geometrixx/am/BaseAMStock_Settings.xml

试试下面的代码示例:

String path = "/etc/clientlibs/geometrixx/am/BaseAMStock_Settings.xml/jcr:content";
if(session.nodeExists(path)) {
    Node node = session.getNode(path);
    if(node.hasProperty("jcr:data")) {
        Property jcrData = node.getProperty("jcr:data");
        //here you can use one of the methods from javax.jcr.Property:
        // - jcrData.getBinary().getStream();
        // - jcrData.getString();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    相关资源
    最近更新 更多