XStream:如何让 xstream.createObjectInputStream() 寻找特定元素

发布于 2025-01-05 02:18:19 字数 1317 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人迟暮 2025-01-12 02:18:19

这个问题的解决方案其实很微不足道,我都快尴尬了,但无论如何我们还是分享一下这个问题的答案吧。我没有获得 HierarchicalStreamReader 的 API,在深入研究 XStream 的代码后,我设法得到了它的句柄,这就是它的工作原理:

    HierarchicalStreamReader xpp = ...;
    while(xpp.hasMoreChildren()) {
        xpp.moveDown();
        // do something with the child element, and recursive into this again if appropriate
        xpp.moveUp();
    }

这是我现在用来寻找适当元素的递归方法。该位置由某种 xpath 表达式(当然是非常基本的)指定,我已提前将其规范化为 ArrayList:

private void seekToElement(HierarchicalStreamReader xpp, ArrayList<String> xpathishArray) {
    while(xpp.hasMoreChildren()) {
        xpp.moveDown();
        if(xpp.getNodeName().equals(xpathishArray.get(0))) {
            if(xpathishArray.size() > 1) {
                xpathishArray.remove(0);
                seekToElement(xpp, xpathishArray);
            }
            return;
        }
        xpp.moveUp();
    }
}

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:

    HierarchicalStreamReader xpp = ...;
    while(xpp.hasMoreChildren()) {
        xpp.moveDown();
        // do something with the child element, and recursive into this again if appropriate
        xpp.moveUp();
    }

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:

private void seekToElement(HierarchicalStreamReader xpp, ArrayList<String> xpathishArray) {
    while(xpp.hasMoreChildren()) {
        xpp.moveDown();
        if(xpp.getNodeName().equals(xpathishArray.get(0))) {
            if(xpathishArray.size() > 1) {
                xpathishArray.remove(0);
                seekToElement(xpp, xpathishArray);
            }
            return;
        }
        xpp.moveUp();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文