使用SAX解析具有未知递归量的XML结构 [英] Parsing an XML structure with an unknown amount of recursions using SAX

查看:104
本文介绍了使用SAX解析具有未知递归量的XML结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用SAX解析器解析JAVA中的XML结构。

I have to parse a XML structure in JAVA using the SAX parser.

问题是结构是递归的,具有未指定的递归计数。这仍然不是什么大问题,最重要的是我无法利用XML命名空间功能,并且每个递归级别的标签都是相同的。

The problem is that the structure is recursive with an unspecified count of recursions. This still is not such a big deal, the big deal is that I can't take advantage of the XML namespace functionality and the tags are the same on every recursion level.

以下是结构示例。

<?xml version="1.0" encoding="UTF-8"?>
<RootTag>
    <!-- LOADS OF OTHER TAGS -->
    <Tags attribute="value">
        <Tag attribute="value">
            <SomeOtherTag></SomeOtherTag>
            <Tags attribute="value">
                <Tag attribute="value">
                    <SomeOtherTag></SomeOtherTag>
                    <Tags attribute="value">
                        <!-- MORE OF THE SAME STRUCTURE -->
                    </Tags>
                </Tag>
            </Tags>
        </Tag>
    </Tags>
    <!-- LOADS OF OTHER TAGS -->
</RootTag>

正如您所看到的那样,有一个递归,更好的是一个未定义的递归数。现在我的问题是如何为每次递归提取所有数据并将其保存在 HashMap 中。例如。

As you can see there is a recursion, better an undefined number of recursions. Now my problem is how to extract all data for every recursion and save it in a HashMap for example.

I可以为标签的出现定义 ContentHandler 并让它在 HashMap中提取内容并将其放回主内容处理程序中定义的 master <$ em $ c> HashMap 但我不确定要做什么热这个。

I could define a ContentHandler for the occurrence of Tags and have it extract the content in a HashMap and put it back in a master HashMap defined in the main content handler but I'm not sure hot to do this.

如何在不使用命名空间的情况下提取和保存递归XML结构的内容?

How do I extract and save the content of a recursive XML structure without using namespaces?

推荐答案

查看这套关于使用SAX的Javaworld文章。它演示了使用SAX解析递归XML结构的简单方法。它创建一个状态机,为每个元素显示它可以包含哪些元素。当你的contentHandler遍历xml时,它会保持一个堆栈,显示它当前所在的元素。

Check out this set of Javaworld articles on using SAX. It demonstrates an easy way to parse a recursive XML structure using SAX. It creates a state machine showing for each element which elements it can contain. As your contentHandler traverses the xml it keeps a stack showing which element it's currently on.

这篇关于使用SAX解析具有未知递归量的XML结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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