使用XQuery和Java将元素插入XML [英] Insert element into XML using XQuery and Java

查看:127
本文介绍了使用XQuery和Java将元素插入XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用XQuery插入表达式将新元素插入XML文档.我正在将saxon用作Java API.我是XQuery的新手,所以我不确定插入表达式的确切结构.谁能帮我这个忙.

I need to insert a new element into an XML document using XQuery insert expression. I am using saxon as a java api. I am new to XQuery so I am not sure about the exact structure of the insert expression. Can anyone help me in this please.

我的XML文件如下所示:

My XML file looks like the following:

<?xml version="1.0" encoding="ISO-8859-1"?>
<breakfast_menu>
<food>
 <name>Belgian Waffles</name>
 <price>$5.95</price>
 <description>two of our famous Belgian Waffles with plenty of real maple syrup<description>
 <calories>650</calories>
</food>
 </breakfast_menu>

我执行插入操作的Java代码是

and the java code I have for executing the insertion is

public void insert() throws XQException{
    String queryString =
            //"declare variable $docName as xs:string external;"  + sep +
            "variable $stores := doc("+"food.xml"+")/*;"+
            "insert node element food {"+
            "element name { \"brnj\" },"+
            "element price { \"$20\" },"+
            "element description { \"whatever\" },"+
            "element calories { 300 },"+
            "} into $stores;";

 XQPreparedExpression expr = conn.prepareExpression(queryString);
 expr.bindObject(new QName("stores"), "food.xml", null);
 XQResultSequence rs = expr.executeQuery();
}

我得到的错误是查询字符串的语法. 请帮忙.

the error I get is in the syntax of the query string. Please help.

推荐答案

BaseX XQJ API支持XQuery Update Facility 1.0和3.0,这使您可以更新BaseX数据库中的文档.

BaseX XQJ API supports XQuery Update Facility 1.0 and 3.0, which allows you to update documents in the BaseX database.

您还可以通过MarkLogic XQJ API,Sedna XQJ API和eXist XQJ API更新数据库内文档,但不能通过XQuery Update Facility更新数据库,因为这些实现尚不支持XQUF.

You can also update in-database documents via the MarkLogic XQJ API, Sedna XQJ API and eXist XQJ APIs although not via XQuery Update Facility, as these implementations do not support XQUF (yet).

最后,可能是最重要的问题,您可能会发现Ryan Grimm的"in-mem-update"库模块非常有用[1].我过去肯定有很多次.

Finally and possibly most importantly for your question, you may find Ryan Grimm's "in-mem-update" library module very useful [1]. I certainly have, countless times in the past.

您可以

  • 将库模块与任何XQuery处理器+ XQJ一起使用
  • 对内存中的XML文档进行更新(无需写入磁盘).
  • 在一个XQuery中对文档执行多个"更新操作
  • 更新后的内存中文档可以XQResultSequence(非常适合您)返回.

此外,只是为了在这里澄清一下,XQJ 1.0规范并未禁止供应商支持XQuery Update Facility.

Also, just to make something clear here, the XQJ 1.0 Specification doesn't disallow vendors from supporting XQuery Update Facility.

摘录自规范:

"18.6支持更新和交易

"18.6 Support for Updates and Transactions

尽管更新功能不是[XQuery]的一部分,但XQJ希望某些功能 实现将包括一些专有扩展来处理更新功能."

Although update functionality is not part of [XQuery], XQJ expects that some implementations will include some proprietary extensions to handle update functionality."

[1] https://github.com/marklogic/commons/blob/master/memupdate/in-mem-update.xqy

这篇关于使用XQuery和Java将元素插入XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆