org.w3c.dom, line number

Posted on 2007-07-30
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
    LVL 2

    Accepted Solution

    i believe your best bet is to use SAX. There is, 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

    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now