Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1055743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:36:37+00:00 2026-05-16T17:36:37+00:00

a HTML file: <html> <body> <div class=main> <p id=tID>content</p> </div> </body> </html> i has

  • 0

a HTML file:

<html>
    <body>
        <div class="main">
            <p id="tID">content</p>
        </div>
    </body>
</html>

i has a String == "content",

i want to use "content" get HTML DOM path:

html body div.main p#tID

chrome developer tools has this feature(Elements tag,bottom bar), i want to know how to do it in java?

thanks for your help 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:36:38+00:00Added an answer on May 16, 2026 at 5:36 pm

    Have fun 🙂

    JAVA CODE

    import java.io.File;
    
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathFactory;
    
    import org.htmlcleaner.CleanerProperties;
    import org.htmlcleaner.DomSerializer;
    import org.htmlcleaner.HtmlCleaner;
    import org.htmlcleaner.TagNode;
    import org.w3c.dom.Document;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    
    
    
    public class Teste {
    
        public static void main(String[] args) {
            try {
                // read and clean document
                TagNode tagNode = new HtmlCleaner().clean(new File("test.xml"));
                Document document = new DomSerializer(new CleanerProperties()).createDOM(tagNode);
    
                // use XPath to find target node
                XPath xpath = XPathFactory.newInstance().newXPath();
                Node node = (Node) xpath.evaluate("//*[text()='content']", document, XPathConstants.NODE);
    
                // assembles jquery/css selector
                String result = "";
                while (node != null && node.getParentNode() != null) {
                    result = readPath(node) + " " + result;
                    node = node.getParentNode();
                }
                System.out.println(result);
                // returns html body div#myDiv.foo.bar p#tID 
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        // Gets id and class attributes of this node
        private static String readPath(Node node) {
            NamedNodeMap attributes = node.getAttributes();
            String id = readAttribute(attributes.getNamedItem("id"), "#");
            String clazz = readAttribute(attributes.getNamedItem("class"), ".");
            return node.getNodeName() + id + clazz;
        }
    
        // Read attribute
        private static String readAttribute(Node node, String token) {
            String result = "";
            if(node != null) {
                result = token + node.getTextContent().replace(" ", token);
            }
            return result;
        }
    
    }
    

    XML EXAMPLE

    <html>
        <body>
            <br>
            <div id="myDiv" class="foo bar">
                <p id="tID">content</p>
            </div>
        </body>
    </html>
    

    EXPLANATIONS

    1. Object document points to evaluated XML.
    2. The XPath //*[text()='content'] finds everthing with text = ‘content’, and find the node.
    3. The while loops up to the first node, getting id and classes of current element.

    MORE EXPLANATIONS

    1. In this new solution I’m using HtmlCleaner. So, you can have <br>, for example, and cleaner will replace with <br/>.
    2. To use HtmlCleaner, just download the newest jar here.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.