JDOM: get line number
Hi,
I'm writting a program were I need each JDOM element's line number i.e. the line where the tag starts because there's a second panel with the xml file's source code and when I doubleclick on a jtree's node (which is in the first panel) the second panel should scroll to the tag which caused this node in the jtree.
Can someone help me? I have absolutely no idea how to do that.
[405 byte] By [
fischefra] at [2007-10-2 1:17:34]

Do you know about the JTree tutorial from Sun? http://java.sun.com/docs/books/tutorial/uiswing/components/tree.htmlGood luck.
You didn't understand the question, did you?My problem is not the JTree! I want the DOM parser to give me the line number of each node.
> You didn't understand the question, did you?
There is no real question, only a vague "can anyone help me?"
> My problem is not the JTree! I want the DOM parser to
> give me the line number of each node.
That's not clear to me from you post.
But good luck with it.
Ok, I try to express it a little better:
When I parse a xml file I get a document with nodes and what I have to know is in which line of the source file was the start tag for a node.
Purpose: I have a window with to panels: in the left one is a JTree which represents my xml file. In the right panel is a listing of the xml file and I have to scroll to the source text when a node in the left panel is clicked.
Does the JDOM have an option to attach a line number to each tag at an attribute?
If not, then the only thing you have is the tag name, and the original file. You could try to do an indexOf(tagName) and count the number of linebreaks between. You'd have to account for tags with the same name, along with attributes and text that may contain the substring. After an indexOf(tagName), scanning left for '<' and right for '> may increase your chances. It's not a very good solution.
I assume you have no control on how the XML file is built, otherwise you could just insert a line number attribute yourself before parsing it.
Why did you choose to use JDOM?
If JDOM can't do it, you could write your own reading filter (extended from InputStream) which addes the line number as attribute to every element (if you discard XML validation).
Thanks, but the problem is already solved.
I decided to implement a kind of pre-parsing which appends the line number to a list every time when a < was found. Later on when I traverse the DOM tree I can use a iterator to read the line number.
Seems to work. Of course I had to remove things like comments, CDATA sections etc to avoid falsified results.
Just think of things like "<--comment <****/> comment end-->"
I don't like that solution. It is too limited in such a way that comments can't be used. Is there any other way or other xml api that allows me to get the line numbers of xml?
Never mind. I found that SAX can do something with line numbers.