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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:47:07+00:00 2026-06-09T04:47:07+00:00

I need to parser an Xml Document and store the values in Text File,

  • 0

I need to parser an Xml Document and store the values in Text File, when i am parsing normal data (If all tags have data) then its working fine, but if any tag doesnt have data then it throwing “Null pointerException” what i need to do, to avoid null pointer exception, please suggest me with sample codes
Sample xml:

<company>
    <staff>
        <firstname>John</firstname>
        <lastname>Kaith</lastname>
        <nickname>Jho</nickname>
        <Department>Sales Manager</Department>
    </staff>
    <staff>
        <firstname>Sharon</firstname>
        <lastname>Eunis</lastname>
        <nickname></nickname>
        <Department></Department>
    </staff>
    <staff>
        <firstname>Shiny</firstname>
        <lastname>mack</lastname>
        <nickname></nickname>
        <Department>SAP Consulting</Department>
    </staff>
</company>

code:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

    public static void main(String argv[]) {

      try {

        File fXmlFile = new File("c:\\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("staff");
        System.out.println("-----------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

           Node nNode = nList.item(temp);
           if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;

              System.out.println("First Name : " + getTagValue("firstname", eElement));
              System.out.println("Last Name : " + getTagValue("lastname", eElement));
                  System.out.println("Nick Name : " + getTagValue("nickname", eElement));
              System.out.println("Salary : " + getTagValue("Department", eElement));

           }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
  }

  private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

    return nValue.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-06-09T04:47:09+00:00Added an answer on June 9, 2026 at 4:47 am

    Just check that the object is not null:

    private static String getTagValue(String tag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if(nValue == null) 
            return null;
        return nValue.getNodeValue();
    }
    
    String salary = getTagValue("Department", eElement);
    if(salary != null) {
        System.out.println("Salary : " + getTagValue("Department", eElement));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse a bunch of incoming xml documents, they all have the
here's my task. I've parsed an xml document and stored all of its data
I use NSXMLParser for parsing an XML document. I have the following functions (among
I have an XML document that has HTML tags included: <chapter> <h1>title of content</h1>
I have a xml document stored in a file on the server, now i
I need an XML parser like SimpleXML, to use in commercial apps. I think
HI, i need a XML parser. i want to know that which one is
Possible Duplicate: Best XML Parser for PHP Using JAXB with Google Android I need
I'm writing a parser for an internal xml-based metadata format in python. I need
Basically I have a script that updates an xml document in various places... However,

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.