Is it possible to get the current line number when parsing an XML document using Xerces?

I have a Java program that parses an XML document using the xerces APIs.

My parsing class extends org.apache.xerces.parsers.XMLDocumentParser, overloading the startElement, endElement, characters methods.

Since this is a complex XML document written by hand (mostly some configuration items), classical verification using xsd or dtd is not enough, and I must return to the user that the XML document is invalid.

But one thing that I could not achieve was to add information to the error messages about the row number (and why not the column number too), which is processed by currenlty and where the error occurs.

It seems to me that this is possible because the Exceptions (org.apache.xerces.xni.parser.XMLParseException) thrown by the parser when the XML document does not contain XML contain this data.

+3
source share
2 answers

I have never tried this with xerces, but SAX analyzers can store a SAX Locator from which you can get row and column numbers when parsing a document (or after an exception).

, XMLDocumentParser . AbstractXMLDocumentParser startDocument, XMLLocator. , XMLLocator getLineNumber getColumnNumber.

+5

, "", API, , XMLInputSource, InputStream Reader, InputStream/Reader, LineNumberInputStream LineNumberReader, .

:

InputStream stream;

stream = ...;

new XMLInputSource(stream);

:

InputStream stream;
LineNumberInputStream lineStream;

stream = ...;
lineStream = new LineNumberInputStream(lineStream);

new XMLInputSource(lineStream);

// can now ask the line stream what line it is on via getLineNumber()

, LineNumberInputStream/LineNumberReader , XMLDocumentParser.

, .

, . /, , , , , , .

+1

Source: https://habr.com/ru/post/1703581/


All Articles