在java中流式传输xml

发布于 2025-01-12 16:58:04 字数 1192 浏览 1 评论 0原文

我正在尝试读取大型 XML 文件,我只想读取车主,但无法将整个 xml 加载到内存中,该怎么做?

XML 文件:

  <root>
    <message>
        <car>
            <owner>adam</owner>
        </car>
        <desk>
            <owner>sam</owner>
            <game>
               <owner>dorothy</owner>
            </game>
            <pen>
               <owner>dorothy</owner>
            </pen>
        </desk>
    </message>
</root>

例如,这段代码并不确切知道它读取的内容。如何确保我们正在读取车主信息?

 XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(entry.toFile()));

        while (reader.hasNext()) {
            XMLEvent nextEvent = reader.nextEvent();

            if (nextEvent.isStartElement()) {
                StartElement startElement = nextEvent.asStartElement();
                log.info(startElement.getName().toString());

                switch (startElement.getName().getLocalPart()) {
                    case "owner":
                        // whose owner. .. ?

I am trying to read large XML file, I want only to read cars owners and I can't load whole xml to memory, how to do that ?

The XML file:

  <root>
    <message>
        <car>
            <owner>adam</owner>
        </car>
        <desk>
            <owner>sam</owner>
            <game>
               <owner>dorothy</owner>
            </game>
            <pen>
               <owner>dorothy</owner>
            </pen>
        </desk>
    </message>
</root>

For example this code does not know exactly what it reads.. how to be sure that we are reading car owners ?

 XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(entry.toFile()));

        while (reader.hasNext()) {
            XMLEvent nextEvent = reader.nextEvent();

            if (nextEvent.isStartElement()) {
                StartElement startElement = nextEvent.asStartElement();
                log.info(startElement.getName().toString());

                switch (startElement.getName().getLocalPart()) {
                    case "owner":
                        // whose owner. .. ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧故 2025-01-19 16:58:04

坚固但可行的解决方案是创建一个小型状态机,捕获事件并相应地改变状态

  1. 如果进入汽车节点 - 存储汽车参考
  2. 如果进入所有者节点并且您之前已输入汽车节点,则存储汽车的所有者
  3. 当退出汽车节点时返回车主对
  4. 重复并处理嵌套和/或节点级别以仅接受车>车主。

Sturdy but viable solution is to create a small state machine, capture events as they go and mutate state accordingly

  1. If entering car node - store car reference
  2. If entering owner node AND you have entered car node previously, store owner of a car
  3. When exiting car node return car-owner pair
  4. Repeat and handle nesting and/or node level to accept only car>owner.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文