0

我得到了这个例外:

javax.xml.stream.XMLStreamException: Attribute not associated with any element
    at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeAttribute(Unknown Source)
    at de.dhbw.horb.routePlanner.parser.GraphDataParser$2.run(GraphDataParser.java:136)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)*

从我下面的代码。我只是不明白它来自哪里。

public void writeEdgeXML() throws XMLStreamException {

    final long[] idCount = new long[1];

    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        final XMLStreamWriter writer = factory
                .createXMLStreamWriter(new FileOutputStream(
                        GraphDataConstants.CONST_XML_EDGE), "UTF-8");

        writer.writeStartDocument("UTF-8", "1.0");

        while (graphSR.hasNext()) {
            if (graphSR.nextStartElement() && graphSR.isWay()) {

                final Way nextWay = getWay(null);

                Controller.executor.getExecutor().submit(new Runnable() {

                    @Override
                    public void run() {

                        while (nextWay != null && nextWay.hasEdge()) {

                            try {
                                idCount[0]++;
                                Edge e = nextWay.removeFirstEdge();
                                writer.writeStartElement(GraphDataConstants.CONST_EDGE);
                                writer.writeAttribute(
                                        GraphDataConstants.CONST_EDGE_ID,
                                        String.valueOf(idCount[0]));
                                writer.writeEmptyElement(GraphDataConstants.CONST_EDGE_NODE);
                                writer.writeAttribute(
                                        GraphDataConstants.CONST_EDGE_ID,
                                        String.valueOf(e.getStartNode()
                                                .getID()));
                                writer.writeAttribute(
                                        GraphDataConstants.CONST_EDGE_LATITUDE,
                                        String.valueOf(e.getStartNode()
                                                .getLatitude()));
                                writer.writeAttribute(
                                        GraphDataConstants.CONST_EDGE_LONGITUDE,
                                        String.valueOf(e.getStartNode()
                                                .getLongitude()));                                  

                                writer.writeEndElement();
                                writer.flush();

                            } catch  (XMLStreamException e) {
                                   e.printStackTrace();
                            }

                        }
                    }
                });
            }
        }
4

1 回答 1

1

XMLStreamWriter不是线程安全的。

您正在创建一个XMLStreamWriter,然后尝试使用它一次在多个线程中写入元素。这根本不能保证有效,如果它确实有效,您创建的元素和属性不一定会在您期望的地方结束。我怀疑错误归结为以下竞争条件

Thread 1             Thread 2
--------             --------
1) emptyElement
2) attribute
                     3) emptyElement
4) endElement
                     5) attribute (BANG!)

您需要将所有 XML 写入序列化到一个线程中。

于 2014-06-25T16:09:54.570 回答