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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:50:58+00:00 2026-06-18T08:50:58+00:00

I want to loop through all elements in a piece of XML printing each

  • 0

I want to loop through all elements in a piece of XML printing each one. My problem is that I keep getting a null pointer exception after the staff1 tag, i.e. john 465456433 gmail1 area1 city1

This my Java code to print all elements in an xml file:

File fXmlFile = new File("file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("*");

System.out.println("----------------------------");

Node n=null;
Element eElement=null;

for (int i = 0; i < nList.getLength(); i++) {           
  System.out.println(nList.getLength());     
  n= nList.item(i);                            
  System.out.println("\nCurrent Element :" + n.getNodeName());


  if (n.getNodeType() == Node.ELEMENT_NODE) {
    eElement = (Element) n.getChildNodes();
    System.out.println("\nCurrent Element :" + n.getNodeName());
    name = eElement.getElementsByTagName("name").item(i).getTextContent(); //here throws null pointer exception after printing staff1 tag
    phone = eElement.getElementsByTagName("phone").item(i).getTextContent();
    email = eElement.getElementsByTagName("email").item(i).getTextContent();
    area = eElement.getElementsByTagName("area").item(i).getTextContent();
    city = eElement.getElementsByTagName("city").item(i).getTextContent();
  }
  n.getNextSibling();
}

XML File:

<?xml version="1.0"?>
<company>
  <staff1>
    <name>john</name>
    <phone>465456433</phone>
    <email>gmail1</email>
    <area>area1</area>
    <city>city1</city>
  </staff1>
  <staff2>
    <name>mary</name>
    <phone>4655556433</phone>
    <email>gmail2</email>
    <area>area2</area>
    <city>city2</city>
  </staff2>
  <staff3>
    <name>furvi</name>
    <phone>4655433</phone>
    <email>gmail3</email>
    <area>area3</area>
    <city>city3</city>
  </staff3>
</company>

Expected Output:

john
465456433
gmail1
area1
city1
mary
4655556433
gmail2
area2
city2
furvi
4655433
gmail3
area3
city3
  • 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-06-18T08:50:59+00:00Added an answer on June 18, 2026 at 8:50 am
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse("file.xml");
        Element docEle = dom.getDocumentElement();
        NodeList nl = docEle.getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) nl.item(i);
                if (el.getNodeName().contains("staff")) {
                    String name = el.getElementsByTagName("name").item(0).getTextContent();
                    String phone = el.getElementsByTagName("phone").item(0).getTextContent();
                    String email = el.getElementsByTagName("email").item(0).getTextContent();
                    String area = el.getElementsByTagName("area").item(0).getTextContent();
                    String city = el.getElementsByTagName("city").item(0).getTextContent();
                }
            }
        }
    

    Iterate over all children and nl.item(i).getNodeType() == Node.ELEMENT_NODE is used to filter text nodes out. If there is nothing else in XML what remains are staff nodes.

    For each node under stuff (name, phone, email, area, city)

     el.getElementsByTagName("name").item(0).getTextContent(); 
    

    el.getElementsByTagName("name") will extract the “name” nodes under stuff,
    .item(0) will get you the first node
    and .getTextContent() will get the text content inside.

    Edit:
    Since we have jackson I would do this in a different way. Define a pojo for the object:

    public class Staff {
        private String name;
        private String phone;
        private String email;
        private String area;
        private String city;
    ...getters setters
    }
    

    Then using jackson:

        JsonNode root = new XmlMapper().readTree(xml.getBytes());
        ObjectMapper mapper = new ObjectMapper();
        root.forEach(node -> consume(node, mapper));
    
    
    
    private void consume(JsonNode node, ObjectMapper mapper) {
        try {
            Staff staff = mapper.treeToValue(node, Staff.class);
            //TODO your job with staff
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to loop through all my input elements and select elements in one
Introduction: I want to loop through XML files with flexible categories structure. Problem: I
I want to loop through all the elements in the DOM structure of a
Suppose I have a vector<int> myvec and I want to loop through all of
I want to loop through the checkboxgroup 'locationthemes' and build a string with all
I have a list that I want to loop through (for trimtemp in trim)
I have a listbox and I want to loop through each of the items
I can't find a solution. How can I loop through all the elements starting
I want to hide all the elements on the page that end in _dropMenu
I would like to loop through all elements of type .dxpc-content img if it

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.