1

Woodstox を使用して XML を記述するのは初めてで、EndDocument を追加するとエラーが発生します。

私のコード:

private static XMLOutputFactory oF = com.ctc.wstx.stax.WstxOutputFactory.newInstance();
private static XMLInputFactory iF = com.ctc.wstx.stax.WstxInputFactory.newInstance();
iF.setProperty(XMLInputFactory.SUPPORT_DTD, false);
iF.setProperty("javax.xml.stream.isSupportingExternalEntities", false);

protected OutputStream writeXML(InputStream iS) throws XMLStreamException {
  ByteArrayOutputStream oS = new ByteArrayOutputStream();
  try {
    XMLEventWriter eW;

    XMLEventFactory eF = com.ctc.wstx.stax.WstxEventFactory.newInstance();
    eW = oF.createXMLEventWriter(oS);
    eW.add(eF.createStartDocument(););

    XMLEventReader eR = iF.createXMLEventReader(iS);
    while(eR.hasNext()) {
      XMLEvent event = eR.nextEvent();
      if (event.isStartElement()) {
      }
      else if (event.isEndElement()) {
      }
      else if (event.getEventType() != XMLEvent.START_DOCUMENT && event.getEventType() != XMLEvent.END_DOCUMENT) {
        if (event.isStartElement() {
          eW.add(event);
        }
        else if (event.isEndElement()) {
          eW.add(event);
        }
      }
      eR.close();
    }
    eW.add(eF.createEndDocument());
    eW.close();
  }
  catch (Exception e) { e.printStackTrace(); }
  return oS;

}

エラー:

javax.xml.stream.XMLStreamException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document).
at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1589)
at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1618)
at com.ctc.wstx.sw.BaseStreamWriter._finishDocument(BaseStreamWriter.java:1444)
at com.ctc.wstx.sw.BaseStreamWriter.writeEndDocument(BaseStreamWriter.java:550)
at org.codehaus.stax2.ri.Stax2EventWriterImpl.add(Stax2EventWriterImpl.java:66)

XML は次のように記述されます。

<?xml version="1.0"?>
  <manifest:manifest xmlns:manifest="a" manifest:version="1.2">
    <manifest:file-entry manifest:full-path="/" manifest:media-type="b.text" manifest:version="1.2"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="c.xml" manifest:media-type=""></manifest:file-entry>
    <manifest:file-entry manifest:full-path="d/" manifest:media-type="e"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="f.png" manifest:media-type="image/png"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="g.xml" manifest:media-type="text/xml"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="h.xml" manifest:media-type="text/xml"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="i.xml" manifest:media-type="text/xml"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="j.rdf" manifest:media-type="application/rdf+xml"></manifest:file-entry>
    <manifest:file-entry manifest:full-path="l.xml" manifest:media-type="text/xml"></manifest:file-entry>

  </manifest:manifest>

プロパティ P_OUTPUT_VALIDATE_STRUCTURE、P_OUTPUT_VALIDATE_CONTENT、P_OUTPUT_VALIDATE_NAMES、P_OUTPUT_VALIDATE_ATTR、P_OUTPUT_FIX_CONTENT を false にして検証を無効にしようとしましたが、次のエラーが発生します。

javax.xml.stream.XMLStreamException: No open start element, when trying to write end element
at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1589)
at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1618)
at com.ctc.wstx.sw.BaseNsStreamWriter.doWriteEndTag(BaseNsStreamWriter.java:664)
at com.ctc.wstx.sw.BaseNsStreamWriter.writeEndElement(BaseNsStreamWriter.java:291)
at com.ctc.wstx.sw.BaseStreamWriter._finishDocument(BaseStreamWriter.java:1457)
at com.ctc.wstx.sw.BaseStreamWriter.writeEndDocument(BaseStreamWriter.java:550)
at org.codehaus.stax2.ri.Stax2EventWriterImpl.add(Stax2EventWriterImpl.java:66)

P_AUTOMATIC_END_ELEMENTS を無効にすると、最初のエラーが発生します。

私は何を間違っていますか?

4

1 に答える 1