Java 如何使用XPath查询更新Jackrabbit中的XML内容?

Java 如何使用XPath查询更新Jackrabbit中的XML内容?,java,xml,xpath,jackrabbit,jcr,Java,Xml,Xpath,Jackrabbit,Jcr,我正在使用Jackrabbit 2.4.0,更新使用以下方法导入的XML时遇到问题: session.importXML(node.getPath(), is, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW); session.save(); 我有以下执行XML更新的代码: public void updateXML(InputStream is, String nodePath) throws RepositoryException, I

我正在使用Jackrabbit 2.4.0,更新使用以下方法导入的XML时遇到问题:

session.importXML(node.getPath(), is, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
session.save();
我有以下执行XML更新的代码:

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();
    }
}
假设我在
/notes
下将以下内容导入Jackrabbit:

<?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>
我得到以下错误:

    org.apache.jackrabbit.spi.commons.conversion.MalformedPathException: '//notes' is not a valid path. double slash '//' not allowed.
但是,我确实导入了以下内容:

/
/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
我只有一个条目

假设我想用ID123更新注释,我将如何使用XPath来进行更新


提前感谢您的帮助

Session.importXml
将字符串
path
作为其第一个参数。因此,您应该指定导入节点的位置


在你的情况下,我认为这是
/notes

你所说的“不起作用”是什么意思?您收到错误消息了吗?还是更改被忽略了?@Robert,我已经更新了我的问题。确实正确!谢谢!:)您是否可以给我一个例子,说明如何按照上面更新的问题中提到的id进行更新?实际上,没有。这会添加另一个带有更新节点的条目。旧的还在那里。想法是将其替换。您是否尝试过使用
importuidbehavior.IMPORT\UUID\u COLLISION\u REPLACE\u EXISTING
而不是
importuidbehavior.IMPORT\UUID\u CREATE\u NEW
?正如上面的代码所示,这就是我在尝试更新时所做的。然而,我相信我需要一个更复杂的XPath,但我不确定它应该是什么样子。在导入之前,您可能需要自己删除节点。
/
/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