carlspring
carlspring

Reputation: 32687

How do you update XML content in Jackrabbit using an XPath query?

I am using Jackrabbit 2.4.0 and I am having trouble updating XML which has been imported using:

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

I have the following code that does the XML updating:

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();
    }
}

Assuming I have a the following imported to Jackrabbit under /notes:

<?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>

Why does the following code to update the XML not work:

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

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

and I get the following error:

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

I do however have the following imported:

/
/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

I have one single entry.

Let's say I would like to update the note with id 123, how would I go about doing that using XPath?

Thanks in advance for your help!

Upvotes: 2

Views: 478

Answers (1)

Robert Munteanu
Robert Munteanu

Reputation: 68308

Session.importXml takes a String path as its first argument. As such, you should specify the location where to import the nodes.

In your case, I believe that is /notes.

Upvotes: 1

Related Questions