【问题标题】:Accessing XML Files from the DAM in CQ5在 CQ5 中从 DAM 访问 XML 文件
【发布时间】:2014-04-14 06:17:28
【问题描述】:

我需要从 DAM 中获取用户上传的 XML 文件,解析该文件并将内容存储在 JCR 中。这是我目前所拥有的

public class foo implements Runnable {
private static final Logger log = LoggerFactory
        .getLogger(foo.class);

@Reference
ResourceResolverFactory resourceResolverFactory;
@Reference
ResourceProvider resourceProvider;
ResourceResolver resourceResolver = null;
@Reference
SlingRepository repository;
Session session;

// private static ReadXMLFileUsingDomparserTest readxml;
File tempFile;

public void run(){
    log.info("\n ***  Seems okay ***\n");
    ResourceResolver resourceResolver = null;

    try {
        resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

        Resource resource =  resourceResolver.getResource("/content/dam/foo/file.xml");
        Node node = resource.adaptTo(Node.class);

        boolean isAssest = DamUtil.isAsset(resource);
        if (isAssest) {
            Asset asset = resource.adaptTo(Asset.class);
            List<Rendition> rendition = asset.getRenditions();
            for (Rendition re : rendition) {
                InputStream in = re.getStream();

                File xmlFile = copy(in,tempFile);
                if(filetest.exists()){
                    ReadXMLFileUsingDomparserTest.parseXML(filetest,null);
                }else {
                    log.info("File not found at all");
                }               
            }       
        }

        File xmlFile = copy(in,tempFile);*/

        }catch (Exception e) {
            log.error("Exception while running foo" , e);
        }
}

private File copy(InputStream in, File file) {
    try {
        OutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return file;
}
}

虽然我能够正确获取 Node 对象(执行 Node.getPath() 返回正确的路径),但我无法将此节点转换为 File 对象。 (无法改编)。我想以 File 对象的形式访问它以进行解析。这就是为什么我浏览资产的再现并使用流将其复制到文件中的原因。

但是,对于上述代码,这始终显示为 null;输出总是File not found at all

从 DAM 获取包含必要数据的 File 对象以便成功解析它的正确方法是什么?

【问题讨论】:

    标签: java xml file-io aem dam


    【解决方案1】:

    上传的 xml 文件应该有一个 nt:file 节点,该节点有一个 jcr:content 节点和 jcr:data 属性。您可以从 jcr:data 读取 xml,即:jcrContent.getProperty("jcr:data").getBinary().getStream();

    这是内置适配器:http://dev.day.com/docs/en/cq/current/developing/sling-adapters.html

    我想你可以在这里使用 InputStream...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多