如何使用 Java 正确迭代 xml?

发布于 2024-12-22 00:57:01 字数 1352 浏览 2 评论 0原文

我有一个 XML 文件开始:

<?xml version="1.0"?>
<results>
    <result id="0001">
        <hometeam>
            <name>Dantooine Destroyers</name>
            <score>6</score>
        </hometeam>
        <awayteam>
            <name>Wayland Warriors</name>
            <score>0</score>
        </awayteam>
    </result>
    <result id="0002">
        <hometeam>
            <name>Dantooine Destroyers</name>
            <score>3</score>
        </hometeam>
        <awayteam>...

在一个 java 文件中:

if(event.isStartElement()){
    if(event.asStartElement().getName().getLocalPart().equals(HOME)){
        System.out.println("In hometeam"); // for testing purposes

        event = eventReader.nextEvent(); // I expect <name> element

        if(event.isStartElement()){ // <------------ FALSE
            if(event.asStartElement().getName().getLocalPart().equals(NAME)){....

我希望这个 if 语句对于 元素是正确的,但如果我坚持在 System.out 中。 println(event.isStartElement()) 我得到FALSE....

此外event.getEventType() 返回XMLEvent.CHARACTERS我不明白...任何人都可以看到 为什么?

如有必要,请随意编辑标签/标题和问题。

I have an XML file starting:

<?xml version="1.0"?>
<results>
    <result id="0001">
        <hometeam>
            <name>Dantooine Destroyers</name>
            <score>6</score>
        </hometeam>
        <awayteam>
            <name>Wayland Warriors</name>
            <score>0</score>
        </awayteam>
    </result>
    <result id="0002">
        <hometeam>
            <name>Dantooine Destroyers</name>
            <score>3</score>
        </hometeam>
        <awayteam>...

and in a java file:

if(event.isStartElement()){
    if(event.asStartElement().getName().getLocalPart().equals(HOME)){
        System.out.println("In hometeam"); // for testing purposes

        event = eventReader.nextEvent(); // I expect <name> element

        if(event.isStartElement()){ // <------------ FALSE
            if(event.asStartElement().getName().getLocalPart().equals(NAME)){....

I'd expect this if statement to be true for the <name> element but if I stick in a System.out.println(event.isStartElement()) I get FALSE....

Also event.getEventType() returns XMLEvent.CHARACTERS which I don't understand... Can anybody see why?

Feel free to make edits to tags/title and question if necessary.

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

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

发布评论

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

评论(2

软的没边 2024-12-29 00:57:02

字符意味着 XML 的下一部分是很好的 - 字符(在您的情况下是换行符和缩进) - 低级解析器没有资格为您丢弃它们。它只提供原始事件。正确处理结构是你的工作。

Characters means that next part of XML are well - characters (in your case newline and indentation) - low level parser is unqualified to discardthem for you. it delivers just raw events. It's your work to proces structure correctly.

意中人 2024-12-29 00:57:02

.nextEvent() 调用可能会在 之间引入空格。请注意,在 XML 中,标记之间的所有字符数据(即使是空格或换行符)也可以从 API 访问。

您可以通过打印元素来测试这一点。

在基于 DOM 的 API 中,您通常不会看到该空格(或者您可以轻松地忽略它),但在事件驱动的 API(如 SAX 或 StAX)中,您必须忽略它。

That .nextEvent() call is probably bringing in the whitespace between <hometeam> and <name>. Note that in XML, all character data between tags (even if it's whitespace or newlines) is also accessible from the API.

You can test this by printing the element.

You don't normally see that whitespace with DOM-based APIs (or you can easily ignore it) but with event-driven APIs (like SAX or StAX) you have to ignore it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文