StAX格式良好的XML检查 [英] StAX well-formedness check of XML

查看:109
本文介绍了StAX格式良好的XML检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查XML文档是否格式良好(仅限语法检查)。我没有验证任何架构。
我需要使用StAX执行此操作:

I am checking an XML document for well formedness (only syntax check). I am not validating against any schema. I need to do this using StAX:

我知道我必须解析文件,但如何实施检查?

I do know that I have to parse the file, but how do I implement the check?

XMLInputFactory factory = XMLInputFactory.newInstance(); 
File f = new File(uploadfileName);
FileInputStream inputStream = new FileInputStream(f); 
//Instantiate a reader parsing:
XMLStreamReader reader=factory.createXMLStreamReader(inputStream);

        while(reader.hasNext()){
            //check to be implemented??
            reader.next();
        }


推荐答案

as @Andreas评论,在课程中没有任何例外情况将表明良构消耗所有解析事件。

As @Andreas commented, well-formedness will be indicated by the lack of any exception being thrown in the course of consuming all parsing events.

具体来说, XMLStreamReader.next() 将抛出 XMLStreamException ,用于解析XML中遇到的任何格式错误。如果解析进展到 XMLStreamReader.hasNext() 返回 false 并终止 while 循环而没有触发任何 XMLStreamExceptions ,你可以确定XML格式正确。

Specifically, XMLStreamReader.next() will throw a XMLStreamException for any well-formedness errors encountered in the XML being parsed. If parsing progresses to where XMLStreamReader.hasNext() returns false and terminates your while loop without having triggered any XMLStreamExceptions, you can be sure that the XML is well-formed.

这篇关于StAX格式良好的XML检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆