上下文

我需要解析一个XML。这个XML很大,所以我使用StAx处理我感兴趣的每个元素。我使用JDK附带的默认实现。

问题

当XML元素位于相同类型的另一个元素之前(例如<person>)并且它们之间没有任何字符时,它将跳过第二个元素。因此,如果我有一个接一个的十个人,我只能解组5个人。例如:

<people><person>..</person><person>..</person></people>

我构建了一个测试,以针对封装在countUnmarshalledPersonEntities()方法中的一段代码显示此行为。

问题是,当元素之间存在空格时,例如:
<people><person><id>1</id></person> <person><id>2</id></person></people>

它解组了两个实体,这没关系。

但是,当节点之间没有空格时,例如:
<people><person><id>1</id></person><person><id>2</id></person></people>

第一个解组会跳过下一个打开的标签<person>,然后第二个人将被忽略。我只解析1个实体。

测试
package org.opensource.lab.stream;

import static org.junit.Assert.assertEquals;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class StreamParserProblemTest {
    private XMLInputFactory xmlif;
    private XMLStreamReader xmlStreamReader;
    private Unmarshaller personUnmarshaller;

    private final InputStream xmlStreamPersonsNoSeparated = IOUtils.toInputStream(
            "<people><person><id>1</id></person><person><id>2</id></person></people>"
            );
    private final InputStream xmlStreamWithPersonsWhitespaceSeparated = IOUtils.toInputStream(
            "<people><person><id>1</id></person> <person><id>2</id></person></people>"
            );

    @Before
    public void setUp() throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
        personUnmarshaller = jaxbContext.createUnmarshaller();
        xmlif = XMLInputFactory.newInstance();
    }

    @After
    public void cleanUp() throws Exception {
        if(xmlStreamReader != null) {
            xmlStreamReader.close();
        }
    }

    @XmlRootElement(name = "person")
    static class Person {
        String id;
    }

    @Test
    public void whenNoSpacesBetweenNodes_shouldFind2Persons_FAIL() throws Exception {
        xmlStreamReader = xmlif.createXMLStreamReader(xmlStreamPersonsNoSeparated, "UTF-8");

        int personTagsFound = countUnmarshalledPersonEntities();

        assertEquals(personTagsFound, 2);
    }

    /**
     * I don't know why, but if there's at least one whitespace character between node of the same type it won't skip.
     *
     * @throws Exception in a test
     */
    @Test
    public void whenWithSpacesBetweenNodes_shouldFind2Persons_SUCCESS() throws Exception {
        xmlStreamReader = xmlif.createXMLStreamReader(xmlStreamWithPersonsWhitespaceSeparated, "UTF-8");

        int personTagsFound = countUnmarshalledPersonEntities();

        assertEquals(personTagsFound, 2);
    }

    /**
     * CODE to test.
     *
     * @return number of unmarshalled persons (people).
     * @throws Exception
     */
    private int countUnmarshalledPersonEntities() throws Exception {
        int personTagsFound = 0;

        while (xmlStreamReader.hasNext()) {
            int type = xmlStreamReader.next();

            if (type == XMLStreamConstants.START_ELEMENT && xmlStreamReader.getName().toString().equalsIgnoreCase("person")) {
                personUnmarshaller.unmarshal(xmlStreamReader, Person.class);
                personTagsFound++;
            }
        }

        return personTagsFound;
    }
}

关于代码的问题有什么想法吗?

谢谢。

最佳答案

感谢您附加的单元测试,这确实使理解变得更加容易!

unmarshal上执行xmlStreamReader时,只要有属于您实体的标签,XMLStreamReader就会自己隐式调用next。因此,在关闭person标记后,它将调用next并指向下一个实体的第一个person标记。在下一次迭代中调用xmlStreamReader.next()时,将其跳过。如果您的实体之间存在空白,则不会发生这种情况,因为解析后,您的读者会指向空白。

此修改后的代码对我有用,您的两个单元测试均成功:

    while (xmlStreamReader.hasNext()) {
        if (xmlStreamReader.isStartElement() && xmlStreamReader.getName().toString().equalsIgnoreCase("person")) {
            personUnmarshaller.unmarshal(xmlStreamReader, Person.class);
            personTagsFound++;
        } else {
            xmlStreamReader.next();
        }
    }

10-07 13:27
查看更多