How to get error's line number while validating a XML file against a XML schema

前端 未结 4 1863
清歌不尽
清歌不尽 2021-02-01 21:26

I\'m trying to validade a XML against a W3C XML Schema.

The following code does the job and reports when error occurs. But I\'m unable to get line number of the error. I

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 21:41

    Try using a SAXLocator http://download.oracle.com/javase/1.5.0/docs/api/org/xml/sax/Locator.html Parsers are not required to supply one, but if they do it should report line numbers

    I think your code should include:

     // this will be called when XML-parser starts reading
        // XML-data; here we save reference to current position in XML:
        public void setDocumentLocator(Locator locator) {
            this.locator = locator;
        }
    

    (see http://www.java-tips.org/java-se-tips/org.xml.sax/using-xml-locator-to-indicate-current-parser-pos.html)

    The parser will give you a locator which you can then use to get the line number. It's probably worth printing/debugging when this happens to see if you have a valid locator

提交回复
热议问题