简体   繁体   English

使用BaseX查询XML流/字符串的Java示例

[英]Java example to query XML stream / string using BaseX

I trying to write Java code to use BaseX in local mode to query XML returned by hitting a web service. 我试图编写Java代码以在本地模式下使用BaseX来查询通过单击Web服务返回的XML。

I'm not sure about the different ways of passing the XML. 我不确定传递XML的不同方式。 Should I 我是不是该

  1. bind an external variable with XML data and pass to XQuery, and then use fn:parse() , 将外部变量与XML数据绑定并传递给XQuery,然后使用fn:parse()
  2. use the fetch module, 使用fetch模块,
  3. or is there any other better way of doing it? 还是有其他更好的方法呢?

Is there any other XQuery 3.0 processor with similar capabilities and in built modules? 内置模块中是否还有其他具有类似功能的XQuery 3.0处理器?

Solution 1: Binding an external variable 解决方案1:绑定外部变量

This means you will first have to store the XML data as java variable and then pass it on to BaseX. 这意味着您首先必须将XML数据存储为java变量,然后将其传递给BaseX。 You can certainly do so, it will look something like that: 您当然可以这样做,它看起来像这样:

declare variable $t as xs:string external;
parse-xml($t)

Solution 2: Use fetch() 解决方案2:使用fetch()

fetch:text() and fetch:binary() are streamable, but parse-xml will materialize the string (be careful, the function is called parse-xml() not parse() as stated in your question. fetch:text()fetch:binary()可以流式传输,但是parse-xml将具体化该字符串(注意,该函数称为parse-xml()而不是您的问题所述的parse()

parse-xml(fetch:text("YOUR-URI"))

Solution 3: Getting the data directly within XQuery 解决方案3:直接在XQuery中获取数据

You can also simply retrieve your data from the web service using the http module: 您还可以使用http模块简单地从Web服务检索数据:

http:send-request(<http:request method='get'></http:request>, "YOUR-URI")[2]

Solution 3 seems to me the easiest way, if you don't further need this result in the Java code. 在我看来,解决方案3是最简单的方法,如果您在Java代码中不再需要此结果的话。 Otherwise you might want to stick to solution 1. 否则,您可能要坚持解决方案1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM