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 936167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:16:12+00:00 2026-05-15T21:16:12+00:00

Hey I have an XML file and I would like to navigate to a

  • 0

Hey I have an XML file and I would like to navigate to a given node and grab the name of all Attributes to that node.

For example: (XML File)

<RootName>
    <SubNode>
    <Attribute1>Value 1</Attribute1>
    <Attribute2>Value 2</Attribute2>  
</SubNode> 
</RootName>

Here is my code so far: (Java Code)

File file = new File("data.xml");
try
    {    
        /* Parse File */
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(file);

        /* Find Starting Tag */
        NodeList nodes = doc.getElementsByTagName(StartTag);
        for (int i = 0; i < nodes.getLength(); i++)
        {
            Element element = (Element) nodes.item(i);
            System.out.println(element);

        }

Now I know you can find a specific attribute given the name

String name = element.getAttribute("Attribute1");    

But I would like to find all these names dynamically.

Thanks in advance

-Scott

  • 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-15T21:16:13+00:00Added an answer on May 15, 2026 at 9:16 pm

    What you are looking for are the Elements. Here is a sample on how to get the Elements in an XML:

    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    
    public class DOMElements{
       static public void main(String[] arg){
         try {
           BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
           System.out.print("Enter XML File name: ");
           String xmlFile = bf.readLine();
           File file = new File(xmlFile);
           if(file.exists()){
             // Create a factory
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             // Use the factory to create a builder
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document doc = builder.parse(xmlFile);
             // Get a list of all elements in the document
             NodeList list = doc.getElementsByTagName("*");
             System.out.println("XML Elements: ");
             for (int i=0; i<list.getLength(); i++) {
               // Get element
               Element element = (Element)list.item(i);
               System.out.println(element.getNodeName());
             }
           }
           else{
             System.out.print("File not found!");
           }
         }
         catch (Exception e) {
           System.exit(1);
         }
       }
    }
    

    Also see my comment below your question on how to properly design XML and when to use elements, and when to use attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey. I have an XML file and would like to use PHP to display
Hey all i have an XML file that i am trying to loop though.
Hey I would like to find a given text in an xml that looks
I have a 2.4 MB XML file, an export from Microsoft Project (hey I'm
hey guys, we have a loop that: 1.Loops over several thousand xml files. Altogether
Hey i have the foolowing xml file : <?xml version=1.0?> <accidents> <accident> <org>1</org> <com>194</com>
Hey all - I am using a flipper in my main xml file to
Hey guys, I have an XML file I need to parse, but only for
Hey all i have code to write to an xml doc from asp string
Hey. I want to have a config.xml file for settings in a Python web

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.