XStream:如何让 xstream.createObjectInputStream() 寻找特定元素
我正在尝试使用 XStream 导入大型 XML 文件(~ 1 GB)。我想使用 ObjectInputStream
来读取它们流式传输,但我需要跳过第一个元素,然后向下移动几个元素(见下文,我需要导入 bag_LVC:Woonplaats< /code> 元素)。
我希望有某种方法可以为 xstream 提供一个 Reader
或一个 InputStream
,这些已经被寻找到我感兴趣的元素。我尝试创建自己的 HierarchicalStreamReader 并将其移动到正确的元素,但您只能上下移动:
HierarchicalStreamReader xpp = new XppDriver().createReader(xmlFile);
xpp.moveDown(); // moves to <xb:antwoord>
xpp.move(); // **would move to <xb:producten>, but it private**
xpp.moveDown(); // moves to <xb:producten>
xpp.moveDown(); // moves to <product_LVC:LVC-product>
st = xstream.createObjectInputStream(xpp);
BagWoonplaats bw = (BagWoonplaats) st.readObject();
Does xstream 或任何 XPP 驱动程序有什么方法让您寻找我的感兴趣的元素?
XML
<xb:BAG-Extract-Deelbestand-LVC>
<xb:antwoord>
<xb:vraag>
<!-- more elements here -->
</xb:vraag>
<xb:producten>
<product_LVC:LVC-product>
<bag_LVC:Woonplaats>
<!-- elements used as fields -->
</bag_LVC:Woonplaats>
<bag_LVC:Woonplaats>
<!-- elements used as fields -->
</bag_LVC:Woonplaats>
<!-- and so on -->
I'm trying to use XStream to import large XML files (~ 1 GB). I want to use an ObjectInputStream
to read them streaming, but I need that to skip the first element and then move down a couple elements (see below, I need to import the bag_LVC:Woonplaats
elements).
I'd expect that there is somehow a way to feed xstream a Reader
or a InputStream
which has been seeked up to my element of interest. I've tried to create my own HierarchicalStreamReader
and move it to the right element, but you can only move up and down:
HierarchicalStreamReader xpp = new XppDriver().createReader(xmlFile);
xpp.moveDown(); // moves to <xb:antwoord>
xpp.move(); // **would move to <xb:producten>, but it private**
xpp.moveDown(); // moves to <xb:producten>
xpp.moveDown(); // moves to <product_LVC:LVC-product>
st = xstream.createObjectInputStream(xpp);
BagWoonplaats bw = (BagWoonplaats) st.readObject();
Does xstream or any of the XPP drivers have any way to let you seek to my element of interest?
xml
<xb:BAG-Extract-Deelbestand-LVC>
<xb:antwoord>
<xb:vraag>
<!-- more elements here -->
</xb:vraag>
<xb:producten>
<product_LVC:LVC-product>
<bag_LVC:Woonplaats>
<!-- elements used as fields -->
</bag_LVC:Woonplaats>
<bag_LVC:Woonplaats>
<!-- elements used as fields -->
</bag_LVC:Woonplaats>
<!-- and so on -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题的解决方案其实很微不足道,我都快尴尬了,但无论如何我们还是分享一下这个问题的答案吧。我没有获得 HierarchicalStreamReader 的 API,在深入研究 XStream 的代码后,我设法得到了它的句柄,这就是它的工作原理:
这是我现在用来寻找适当元素的递归方法。该位置由某种 xpath 表达式(当然是非常基本的)指定,我已提前将其规范化为 ArrayList:
The solution for this problem was actually very trivial, and I'm almost embarrassed, but let's share the answer for this problem anyway. I didn't get the API for the HierarchicalStreamReader, after digging around in the code of XStream I managed to get an handled to it, this is how it works:
This is recursive method I now use to seek to the appropriate element. The location is specified by a sort of xpath expression (very basic, of course), which I have normalized in advance, into an ArrayList: