2015-04-01 60 views
0

在从CQ5.4迁移到AEM6期间,我在将XML数据导入到JCR时遇到了问题。AEM6。 XML导入到JCR(Oak)

在CQ5.4上,我们使用“Content Loader Tool”(http(s):// [host]:[port] /crx/loader/index.jsp)将xml加载到jcr。 从CQ5.6.1开始,该工具已被弃用。 AEM6也没有它,像几个crx:Xml *主节点类型一样(crx:XmlCharacterData,crx:XmlDocument,crx:XmlElement,crx:XmlNode)。

我试图以编程方式重新导入数据,下面的示例Groovy脚本

importXML(); 
def importXML(){ 
    FileInputStream inputStream = new FileInputStream("c:/data.xml "); // XML file 
    session.importXML("/content/xmlNode", // Destination JCR node 
     inputStream , 
     javax.jcr.ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW); 
    session.save(); 
} 

但作为进口的结果,我失去了所有的兄弟姐妹数据。 导入的数据在JCR的每个图层上只有一个节点。 原因是橡树不支持同名兄弟姐妹(SNS)。

http://docs.adobe.com/docs/en/aem/6-0/deploy/upgrade/introduction-to-oak.html http://jackrabbit.apache.org/oak/docs/differences.html#Same_name_siblings

我不需要支持SNS或CRX:XML *节点类型。 我很高兴为兄弟姐妹(即node_1,node_2)和主节点类型“nt:unstructured”生成了唯一的生成名称。 或任何其他jcr结构,它保留从XML导入的所有数据。

如何将XML数据导入到AEM6?请帮助我。

回答

0

不幸的是,它看起来像自动创建唯一节点的能力不存在(例如JcrUtils有createUnique方法,将数字添加到冲突的节点名称)。有可能使用XSLT来重命名每个节点,以便它是唯一的。

0

希望这会帮助别人

这里是我如何使用进口商做到了:

1)这里是一个简单的导入类

@Service(value = Importer.class) 
@Component 
@Property(name = "importer.scheme", value = "importedData", propertyPrivate =true) 

public class DataImporter implements Importer { 
private final String SOURCE_URL = "http://someserver/data.xml"; 
private static final Logger LOGGER = LoggerFactory.getLogger(DealerDataImporter.class); 

@Override 
public void importData(String s, String s2, Resource resource) throws ImportException { 

    try { 

     URL url = new URL(SOURCE_URL); 
     URLConnection connection = url.openConnection(); 
     Document doc = parseXML(connection.getInputStream()); 
     NodeList Nodes = doc.getElementsByTagName("retailer"); 
     for (int i = 0; i < Nodes.getLength(); i++) { 
      Element element = (Element) Nodes.item(i); 
      String id = element.getElementsByTagName("id").item(0).getTextContent(); 
      String name = element.getElementsByTagName("display_name").item(0).getTextContent(); 
      String about = element.getElementsByTagName("about").item(0).getTextContent(); 
      writeToRepository(id, name, about, resource); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void writeToRepository(String id, String name, String about, Resource resource) { 

    try { 
     javax.jcr.Node parentNode = resource.adaptTo(javax.jcr.Node.class); 

     //Say we want this to be a Page node (use NameConstants.NT_PAGE = cq:Page) 
     //all of node types can be created this way 
     javax.jcr.Node pageNode = JcrUtil.createPath(parentNode.getPath() + "/" + name, NameConstants.NT_PAGE, parentNode.getSession()); 
     //Page nodes need jcr:content node to hold all teh relevant properties (NameConstants.NN_CONTENT = "jcr:content") 
     javax.jcr.Node contentNode = JcrUtil.createPath(pageNode.getPath() + "/" + NameConstants.NN_CONTENT, "cq:PageContent", parentNode.getSession()); 
     //set some properties 
     contentNode.setProperty("about", about); 
     contentNode.setProperty("id", id); 
     //save session 
     parentNode.getSession().save(); 

    } catch (Exception e1) { 
     e1.printStackTrace(); 
    } 
} 
private Document parseXML(InputStream stream){...} 
} 

2)然后在CRXde 下/ etc/importers/polling添加新节点类型sling:文件夹并添加一些属性:

a)target [String]这是存储库资源的路径,该资源将被解析为您的导入器类。

b)source [String]这是如果你想从多个xml文件导入 重要的是需要启动importedData(作为它们链接的类的一个属性),后面跟着:SOME_VALIABLE这是在上例中s2是可变的。

C)区间[龙]如何运行进口商

d)添加混入JCR:mixinTypes类型CQ:PollConfig

这应该是它到目前为止我可以使用导入任何数据这种技术