首页 > 解决方案 > 我可以在 javax.xml.bind.MarshalException 上捕获 xml 的行号吗?

问题描述

我还有其他类似的错误,有一种方法可以输出未验证的对象字段?

cvc-enumeration-valid:值 '02' 对于枚举 '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 不是方面有效的, 16, 17, 18, 19, 20]'。它必须是来自枚举的值。

public static String marshalValidatingBoPgit(Object el) throws Exception {
    if (el == null) {
        return null;
    }
    Marshaller m = JaxbContextHolder.getInstance().getMarshaller(XmlObject.class);
    m.setSchema(SchemaManager.getSchema("idb/xmlXsd/xml-schema.xsd"));
    m.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event) {
            System.err.println(event);
            return false;
        }
    });
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    m.marshal(el, baos);
    baos.flush();
    String result = new String(baos.toByteArray());
    baos.close();
    return result;
}

标签: javaxmlxsdjaxbmarshalling

解决方案


推荐阅读