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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:45:56+00:00 2026-05-29T05:45:56+00:00

I am learning the Java XML API. I am using DOM. I have a

  • 0

I am learning the Java XML API. I am using DOM.

I have a problem with even basic navigation inside the document. Here is the XML files I am working with:

<?xml version="1.0"?>
<company>
    <staff>
        <firstname>test</firstname>
        <lastname>test2</lastname>
        <nickname>test3</nickname>
        <salary>test4</salary>
    </staff>
    <staff>
        <firstname>test5</firstname>
        <lastname>test6</lastname>
        <nickname>test7</nickname>
        <salary>test8</salary>
    </staff>
</company>

And here is the code that I have so far and supposed to get the name of the parent node and it’s child nodes:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder  = factory.newDocumentBuilder();
Document document = builder.parse(new File(pathtothefile));

Element topLevelElement = document.getDocumentElement();
NodeList secondLevelElements = topLevelElement.getChildNodes();

System.out.println("Top level element: " + topLevelElement.getNodeName());
System.out.println("Number of second level nodes: " + secondLevelElements.getLength());
System.out.println("Node at index 0: " + secondLevelElements.item(0).getNodeValue());

I get number of second level nodes (it is for some reason 5, not 2) but when I try to get the name of node at index 0 I get “#text” or if I try to get the value: nothing displays.

I would appreciate any help as I am a total beginner to all of these and fell kind of lost 🙂

UPDATE 1:

Here is the new code:

Element companyElement = document.getDocumentElement();
NodeList staffElements = companyElement.getElementsByTagName("staff");
NodeList firstNameElements = companyElement.getElementsByTagName("firstname");
NodeList lastNameElements = companyElement.getElementsByTagName("lastname");
NodeList nicknameElements = companyElement.getElementsByTagName("nickname");
NodeList salaryElements = companyElement.getElementsByTagName("salary");

System.out.println("Top level element: " + companyElement.getNodeName());
System.out.println("----");
System.out.println("Next nodes' level name: " + staffElements.item(0).getNodeName());
System.out.println("Next nodes' level number: " + staffElements.getLength());
System.out.println("----");
System.out.println("Person No. 1");
System.out.println("First name: " + firstNameElements.item(0).getNodeValue());
System.out.println("Last name: " + lastNameElements.item(0).getNodeValue());
System.out.println("Nickname: " + nicknameElements.item(0).getNodeValue());
System.out.println("Salary: " + salaryElements.item(0).getNodeValue());
System.out.println("----");
System.out.println("Person No. 2");
System.out.println("First name: " + firstNameElements.item(1).getNodeValue());
System.out.println("Last name: " + lastNameElements.item(1).getNodeValue());
System.out.println("Nickname: " + nicknameElements.item(1).getNodeValue());
System.out.println("Salary: " + salaryElements.item(1).getNodeValue());
  • 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-29T05:45:57+00:00Added an answer on May 29, 2026 at 5:45 am

    I get number of second level nodes (it is for some reason 5, not 2)

    This is because the DOM preserves white space. So what you have at that level is:

    [whitespace][staff element][whitespace][staff element][whitespace]

    i.e. 5 nodes.

    when I try to get the name of node at index 0 I get “#text”

    If you read the javadoc for Node.getName(), you’d know why. The node at index 0 is a whitespace node, and getName() on a text node returns the hard-wired string #text.

    if I try to get the value: nothing displays

    Again, that’s because it’s a whitespace-only text node.

    You need to fetch the nodes at index 1 and 3 if you need to access the <staff> elements.

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

Sidebar

Related Questions

I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
So I'm learning java, and I have a question. It seems that the types
I'm currently learning Java, using a combination of the Head First Java book and
When i started learning android i learned that R.java files goes to /gen folder
I'm learning Java EE 6 and JSF 2.0 on JBoss6 and have built a
hi I started learning google data api. I have one question What will be
I have starting learning Java and android application development side-by-side. Currently, I a String-array
Hello i'm learning a bit JSP and i have a problem with the code
I have just started learning about Java server faces and successfully tested out my
just learning Java and I know this may sound stupid but I have to

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.