How to get linenumber info for xml elements

C

Cengiz

Hi,

i have to parse an xml-file.
I do this with JDOM:

File file = new File("test.xml");
SAXBuilder builder = new SAXBuilder(true);
builder.setFeature("http://apache.org/xml/features/validation/schema", true);
Document document = builder.build(file);
Element rootElement = document.getRootElement();

Now i have the root-element and i can hangle through the document.
But how i can get the line number information for a special element.
For example: Element child = rootElement.getChild("ChildElement");

Corresponding XML-File:
<TEST>
<ChildElement attr="..."/>
...
</TEST>

ChildElement is at line 2.
How to get this information ?

thx, Cengiz
 
C

Christophe Vanfleteren

Cengiz said:
Hi,

i have to parse an xml-file.
I do this with JDOM:

File file = new File("test.xml");
SAXBuilder builder = new SAXBuilder(true);
builder.setFeature("http://apache.org/xml/features/validation/schema",
true); Document document = builder.build(file);
Element rootElement = document.getRootElement();

Now i have the root-element and i can hangle through the document.
But how i can get the line number information for a special element.
For example: Element child = rootElement.getChild("ChildElement");

Corresponding XML-File:
<TEST>
<ChildElement attr="..."/>
...
</TEST>

ChildElement is at line 2.
How to get this information ?

I don't think you can:
line numbers have absolutely no meaning in XML, and it wouldn't make sense
to allow you to retrieve them.

What exactly are you trying to do that makes you need them?
 
T

Tim Ward

Christophe Vanfleteren said:
I don't think you can:
line numbers have absolutely no meaning in XML, and it wouldn't make sense
to allow you to retrieve them.

Being able to tell the user that their error in their hand-written XML file
is on line n would make sense and would have meaning IMO.
 
C

Christophe Vanfleteren

Tim said:
Being able to tell the user that their error in their hand-written XML
file is on line n would make sense and would have meaning IMO.

Correct, but that is the parsers work. This person wants to know the
linenumber when he already has a Document, which can not be created unless
the XML is valid. I also meant that the linenumber has no semantic meaning
in XML.

If it is part of some higher level error, that the DTD or Schema can not
catch, then it could be useful.

But this would never work on the level of a Document. For example, how would
you determine what makes up a line when adding a ChildNode somewhere in the
Document, or what about when you add a Node before others, will you change
the linenumber of all following nodes? What would you do with a Document,
created from an XML string? ...
 
C

Cengiz

Hi Christophe,

ok you're right. Normally in XML the information is essential and not
where (linenumber) it is. But i need this information.
I have to parse several text-files. Therefor i have XML-Files, which
describe the structure of the text-files. These XML-Files are sort of
configuration files. Now it can happen, that these config-files are
logical not correct. I check them with a XML-Scheme, so they are
syntactical OK. If an logical error occurs, it is not obvious, whether
the text-file or the configfile (XML-File) isnt't correct. Therefor my
program should show an error-message in which line which element could
be the error.
 
N

nos

Christophe Vanfleteren said:
I don't think you can:
line numbers have absolutely no meaning in XML, and it wouldn't make sense
to allow you to retrieve them.

I humbly disagree. Java source files do not have line numbers, but
when a stack trace is produced I think people make good use of
the line numbers therein. Likewise, when you are processing an XML
file and get an error message, it is nice to know where to start looking
for the error.
 
C

Christophe Vanfleteren

nos said:
I humbly disagree. Java source files do not have line numbers, but
when a stack trace is produced I think people make good use of
the line numbers therein. Likewise, when you are processing an XML
file and get an error message, it is nice to know where to start looking
for the error.

Ok, I should have been clearer. I should have said: linenumbers have no
meaning in a DOM Document. Also see my answer to Tim in this thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top