Jaxb marshaller实现和处理IOException

时间:2015-04-09 10:50:19

标签: java xml serialization jaxb ioexception

我们在使用JAXB生成XML文件时遇到了一些问题。(即使我们对XSD进行验证,生成的文件也会被破坏/混乱)。但是服务器日志(catalina.out)中没有报告错误

在我们进行调查时,我们碰巧在com.sun.xml.internal.bind.v2.runtime.MarshallerImpl中找到了以下代码段(这是java用来将JAXB对象编组为XML的运行时类) )。

    /**
 * All the marshal method invocation eventually comes down to this call.
 */
private void write(Object obj, XmlOutput out, Runnable postInitAction) throws JAXBException {
    try {
        if( obj == null )
            throw new IllegalArgumentException(Messages.NOT_MARSHALLABLE.format());

        if( schema!=null ) {
            // send the output to the validator as well
            ValidatorHandler validator = schema.newValidatorHandler();
            validator.setErrorHandler(new FatalAdapter(serializer));
            // work around a bug in JAXP validator in Tiger
            XMLFilterImpl f = new XMLFilterImpl() {
                @Override
                public void startPrefixMapping(String prefix, String uri) throws SAXException {
                    super.startPrefixMapping(prefix.intern(), uri.intern());
                }
            };
            f.setContentHandler(validator);
            out = new ForkXmlOutput( new SAXOutput(f) {
                @Override
                public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws SAXException, IOException, XMLStreamException {
                    super.startDocument(serializer, false, nsUriIndex2prefixIndex, nsContext);
                }
                @Override
                public void endDocument(boolean fragment) throws SAXException, IOException, XMLStreamException {
                    super.endDocument(false);
                }
            }, out );
        }

        try {
            prewrite(out,isFragment(),postInitAction);
            serializer.childAsRoot(obj);
            postwrite();
        } catch( SAXException e ) {
            throw new MarshalException(e);
        } catch (IOException e) {
            throw new MarshalException(e);
        } catch (XMLStreamException e) {
            throw new MarshalException(e);
        } finally {
            serializer.close();
        }
    } finally {
        cleanUp();
    }
}

private void cleanUp() {
    if(toBeFlushed!=null)
        try {
            toBeFlushed.flush();
        } catch (IOException e) {
            // ignore
        }
    if(toBeClosed!=null)
        try {
            toBeClosed.close();
        } catch (IOException e) {
            // ignore
        }
    toBeFlushed = null;
    toBeClosed = null;
}

方法write(Object obj,XmlOutput out,Runnable postInitAction)调用finally子句中的cleanup方法,并且正在发生刷新。但是在这种清理方法中,如果发生任何IOException,则在刷新时会忽略该异常。

我们想知道,这些段是否生成了损坏的XML,因为它不会抛弃异常而只是吃掉异常。

此外,我们在XML序列化程序的API文档中找到了以下语句。

class:org.apache.xml.serialize.XMLSerializer

如果在序列化时发生I / O异常,则序列化程序不会直接抛出异常,而只会在序列化结束时抛出异常(DOM或SAX的DocumentHandler.endDocument()。< / em>

感谢任何帮助。

此致 Mayuran

1 个答案:

答案 0 :(得分:0)

IOException不会被忽略它被包裹在MarshalException中并被重新抛出。基本问题应该出现在您收到的MarshalException中。