3

我有一个应用程序JSON -> XML转换器。这个应用程序将接受一个List被一个一个地转换为 XML 的事件。在转换之前,将使用该start方法创建最终 XML 的标头,然后将转换后的事件xmlEventWriter一一添加,最后在所有转换后将结束标记添加到 XML usingend方法。

我在关闭标签并遇到错误时遇到问题:

javax.xml.stream.XMLStreamException: No open start element, when trying to write end element

根据我的理解,一切都是正确的,但仍然面临不知道为什么的问题。

以下是将header body and closure在 XML 中创建标签的类:



public class EventXMLStreamCollector implements EventsCollector<OutputStream> {

    private final OutputStream stream;
    private final XMLEventWriter xmlEventWriter;
    private final XMLEventFactory events;

    public EventXMLStreamCollector(OutputStream stream) {
        this.stream = stream;
        try {
            xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(stream);
            events = XMLEventFactory.newInstance();
        } catch (XMLStreamException e) {
            throw new EventFormatConversionException("Error occurred during the creation of XMLEventWriter : " + e);
        }

    }

    public void collect(Object event) {
        System.out.println("COLLECT START");
        try {
            XMLEventReader xer = new EventReaderDelegate(XMLInputFactory.newInstance().createXMLEventReader(new StringReader(event.toString()))) {
                @Override
                public boolean hasNext() {
                    if (!super.hasNext())
                        return false;
                    try {
                        return !super.peek().isEndDocument();
                    } catch (XMLStreamException ignored) {
                        return true;
                    }
                }
            };
            if (xer.peek().isStartDocument()) {
                xer.nextEvent();
                xmlEventWriter.add(xer);
            }
        } catch (XMLStreamException e) {
            throw new EventFormatConversionException("Error occurred during the addition of events to XMLEventWriter: " + e);
        }
        System.out.println("COLLECT END");
    }

    @Override
    public OutputStream get() {
        return stream;
    }

    @Override
    public void start(Map<String, String> context) {
        System.out.println("START START");
        try {
            xmlEventWriter.add(events.createStartDocument());
            xmlEventWriter.add(events.createStartElement(new QName("doc:Document"), null, null));
            xmlEventWriter.add(events.createNamespace("doc", "urn:one"));
            xmlEventWriter.add(events.createNamespace("xsi", "http://www.w3.org/2001/XMLSchem-instance"));
            xmlEventWriter.add(events.createNamespace("cbvmda", "urn:two"));
            
            for (Map.Entry<String, String> stringStringEntry : context.entrySet()) {
                xmlEventWriter.add(events.createAttribute(stringStringEntry.getKey(), stringStringEntry.getValue()));
            }

            xmlEventWriter.add(events.createStartElement(new QName("Body"), null, null));
            xmlEventWriter.add(events.createStartElement(new QName("EventList"), null, null));
        } catch (XMLStreamException e) {
            throw new EventFormatConversionException("Error occurred during the creation of final XML file header information " + e);
        }
        System.out.println("START END");
    }

    @Override
    public void end() {
        System.out.println("END START");
        try {
            System.out.println(xmlEventWriter.toString());
            xmlEventWriter.add(events.createEndElement(new QName("EventList"), null));
            xmlEventWriter.add(events.createEndElement(new QName("Body"), null));
            xmlEventWriter.add(events.createEndElement(new QName("doc:Document"), null));
            xmlEventWriter.add(events.createEndDocument());
            xmlEventWriter.close();
        } catch (XMLStreamException e) {
            throw new EventFormatConversionException("Error occurred during the closing xmlEventWriter:" + e);
        }
        System.out.println("END END");
    }

    @Override
    public void collectSingleEvent(Object event) {
        try {
            XMLEventReader xer = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(event.toString()));
            if (xer.peek().isStartDocument()) {
                xer.nextEvent();
            }
            xmlEventWriter.add(xer);
        } catch (XMLStreamException e) {
            System.out.println("ADDED : " + e.getMessage());
            throw new EventFormatConversionException("Error occurred during the addition of events to XMLEventWriter: " + e);
        }
    }
}

我收到此行的错误:

xmlEventWriter.add(events.createEndElement(new QName("Body"), null));

我不确定为什么会收到此错误。我正在打开Body标签,然后尝试关闭它。我确信流程是正确的,我正在调用start, collect,然后是 finally end。继output我得到:

START START
START END
COLLECT START
COLLECT END
END START

END END由于closing of Body标签出现错误,我没有收到。有人可以帮我理解这个问题并提供一些解决方法和帮助。

4

0 回答 0