java 定位文件未释放_java-XML验证不会释放xml文件

我正在尝试针对JAVA中的XSD文件验证XML文件.我的问题不是验证本身,因为这可以正常工作.

我的问题是,验证后未释放XMLfile.如果以后尝试访问该文件,则会收到错误“该文件已被其他资源使用”.

仅当验证失败时才会发生此错误(validator.validate(xmlSource)抛出异常)

如果对该文件进行了验证而没有问题,则该文件将被释放并且可以被其他人访问.

有想法吗?

public void validateXMLAgainstXSD(String xmlPath, String xsdPath) throws ParserException, IOException

{

Source xmlSource = null;

File schemaFile = null;

SchemaFactory schemaFactory = null;

Schema schema = null;

Validator validator = null;

try

{

schemaFile = new File(xsdPath);

xmlSource = new StreamSource(new File(xmlPath));

schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

schema = schemaFactory.newSchema(schemaFile);

validator = schema.newValidator();

validator.validate(xmlSource);

}

catch (SAXException e)

{

//_log.error("ParsingDataFile: XML file could not be validated against XSD file: XML File= XSD file=. Exception=");

xmlSource = null;

schemaFile = null;

schemaFactory = null;

schema = null;

validator.reset();

validator = null;

//throw new ParserException(-1, ParserException.ERROR_CODE_XML_NOT_VALID, e);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值