org.w3c.dom, line number

Posted on 2007-07-30
Medium Priority
Last Modified: 2008-02-01
I have this:
InputStream in = new FileInputStream(fileName);
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
Document doc = dfactory.newDocumentBuilder().parse(new InputSource(in));

Given a node belonging to doc,
I would like to know at which line of text (of the file fileName) this node starts.
(I assume I have not modified the document)

Something like
int lineNumber = XmlUtil.getLineNumber(fileName, doc, node);

Are you aware of a xml util package that has this functionality?
(compatible with org.w3c.dom)

Question by:dportabella

Accepted Solution

freeexpert earned 1000 total points
ID: 19594759
i believe your best bet is to use SAX. There is http://xerces.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/impl/xs/opti/ElementImpl.html, which has a getLineNumber method. But I you will have to jump hoops to get to it and it is for Element only.

Author Comment

ID: 19595071
freeexpert, that could be a solution.
Given the doc and node, the xpath expression could be easily obtained,
and then, parsing the file, the element could be found using the xpath expression.

>it is for Element only
that's ok.

However, I wouldn't like to code this function myself.
I am looking for a xml util package that already implements it.

Are you aware of a xml util package that has this functionality?
(compatible with org.w3c.dom)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question