Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You may want to look at <code>Locator</code>, <code>DefaultHandler</code> and <code>SAXParser</code>. As an example which gives line number and column number:</p> <pre><code>public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException { String xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n" + "&lt;project \n"+ "&gt;\n"+ " &lt;description&gt;A description&lt;/description&gt;\n"+ "&lt;/project&gt;\n"; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); InputSource inps = new InputSource(new StringReader(xml)); DefaultHandler df = new XDefaultHandler(); sp.parse(inps, df); } static class XDefaultHandler extends DefaultHandler { Locator l = null; @Override public void setDocumentLocator(Locator locator) { l = locator; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("element: " + qName); System.out.println("locator: " + l.getLineNumber() + "/" + l.getColumnNumber()); } } </code></pre> <p>The output is:</p> <pre><code>element: project locator: 3/2 element: description locator: 4/18 </code></pre> <p>Override other methods in XDefaultHandler to receive calls for end of elements etc.</p> <p>EDIT: (hit the button too early)</p> <p>From the documentation of <code>Locator.getLineNumber()</code>:</p> <blockquote> <p>Return the line number where the current document event ends. Lines are delimited by line ends, which are defined in the XML specification. </p> <p>Warning: The return value from the method is intended only as an approximation for the sake of diagnostics; it is not intended to provide sufficient information to edit the character content of the original XML document. In some cases, these "line" numbers match what would be displayed as columns, and in others they may not match the source text due to internal entity expansion. </p> <p>The return value is an approximation of the line number in the document entity or external parsed entity where the markup triggering the event appears.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload