2015-04-01 62 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

這應該是它到目前爲止我可以使用導入任何數據這種技術