2012-03-26 10 views
2

Ich bin mit Hase 2.4.0 und ich habe Probleme beim XML-Aktualisierung, die unter Verwendung importiert wurde:Wie aktualisierst du XML-Inhalt in Jackrabbit mit einer XPath-Abfrage?

session.importXML(node.getPath(), is, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW); 
session.save(); 

ich den folgenden Code haben, der die XML-Aktualisierung funktioniert:

public void updateXML(InputStream is, String nodePath) 
     throws RepositoryException, IOException, NamingException 
{ 
    Session session = getSession(); 

    try 
    { 
     logger.debug("Updating path '" +nodePath +"'..."); 

     session.importXML(nodePath, is, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); 
     session.save(); 
    } 
    finally 
    { 
     if (session != null) 
      session.logout(); 
    } 
} 

Angenommen, ich habe ein folgendes unter /notes zu Jackrabbit importiert:

<?xml version="1.0"?> 
<note xmlns="http://testuri.org/note" 
     xsi:schemaLocation="http://testuri.org/note file:///path/to/note.xsd" 
     xmlns:nt="http://www.jcp.org/jcr/nt/1.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <id>123</id> 
    <to>Me</to> 
    <from>You</from> 
    <heading>Meeting at five</heading> 
    <body>With this you are hereby invited to the important meeting at five.</body> 

</note> 

Warum wird der folgende Code die XML nicht arbeiten zu aktualisieren:

InputStream is = getClass().getClassLoader().getResourceAsStream("note-update.xml"); 

    serviceXML.updateXML(is, "//notes"); 

und ich erhalte den folgenden Fehler:

org.apache.jackrabbit.spi.commons.conversion.MalformedPathException: '//notes' is not a valid path. double slash '//' not allowed. 

Ich habe jedoch die importiert folgende:

/ 
/jcr:primaryType = rep:root 
/jcr:system 
/notes 
/notes/jcr:primaryType = nt:unstructured 
/notes/note:note 
/notes/note:note/xsi:schemaLocation = http://testuri.org/note file:///path/to/note.xsd 
/notes/note:note/jcr:primaryType = nt:unstructured 
/notes/note:note/note:id 
/notes/note:note/note:id/jcr:primaryType = nt:unstructured 
/notes/note:note/note:id/jcr:xmltext 
/notes/note:note/note:id/jcr:xmltext/jcr:xmlcharacters = 123 
/notes/note:note/note:id/jcr:xmltext/jcr:primaryType = nt:unstructured 
/notes/note:note/note:to 
/notes/note:note/note:to/jcr:primaryType = nt:unstructured 
/notes/note:note/note:to/jcr:xmltext 
/notes/note:note/note:to/jcr:xmltext/jcr:xmlcharacters = Me 
/notes/note:note/note:to/jcr:xmltext/jcr:primaryType = nt:unstructured 
/notes/note:note/note:from 
/notes/note:note/note:from/jcr:primaryType = nt:unstructured 
/notes/note:note/note:from/jcr:xmltext 
/notes/note:note/note:from/jcr:xmltext/jcr:xmlcharacters = You 
/notes/note:note/note:from/jcr:xmltext/jcr:primaryType = nt:unstructured 
/notes/note:note/note:heading 
/notes/note:note/note:heading/jcr:primaryType = nt:unstructured 
/notes/note:note/note:heading/jcr:xmltext 
/notes/note:note/note:heading/jcr:xmltext/jcr:xmlcharacters = Meeting at five 
/notes/note:note/note:heading/jcr:xmltext/jcr:primaryType = nt:unstructured 
/notes/note:note/note:body 
/notes/note:note/note:body/jcr:primaryType = nt:unstructured 
/notes/note:note/note:body/jcr:xmltext 
/notes/note:note/note:body/jcr:xmltext/jcr:xmlcharacters = With this you are hereby invited to the important meeting at five. 
/notes/note:note/note:body/jcr:xmltext/jcr:primaryType = nt:unstructured 

ich einen einzigen Eintrag haben.

Nehmen wir an, ich möchte die Notiz mit ID 123 aktualisieren, wie würde ich das mit XPath machen?

Vielen Dank im Voraus für Ihre Hilfe!

+1

Was meinen Sie mit "funktioniert nicht"? Erhalten Sie eine Fehlermeldung oder wird die Änderung ignoriert? –

+0

@Robert, ich habe meine Frage aktualisiert. – carlspring

Antwort

1

Session.importXml nimmt einen String path als sein erstes Argument. Daher sollten Sie den Speicherort für die Knoten angeben.

In Ihrem Fall glaube ich, dass das /notes ist.

+0

Korrekt in der Tat! Vielen Dank! :) Jede Chance, die Sie mir auch ein Beispiel geben konnten, wie ich durch ID aktualisieren kann, wie in meiner aktualisierten Frage oben erwähnt? – carlspring

+0

Eigentlich nein. Dies fügt einen weiteren Eintrag zu dem aktualisierten Knoten hinzu. Der alte ist immer noch da. Die Idee ist es zu ersetzen. – carlspring

+0

Haben Sie versucht, 'ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING' anstelle von' ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW' zu verwenden? –