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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:29:46+00:00 2026-05-27T09:29:46+00:00

So I have an XML file in the format: <projectlist> <project> <name>test</name> <type>deploy</type> <environment>dev</environment>

  • 0

So I have an XML file in the format:

<projectlist>
   <project>
      <name>test</name>
      <type>deploy</type>
      <environment>dev</environment>
      <server>test01</server>
      <server>test02</server>
      <server>test03</server>
   </project>
</projectlist>

I’m trying to parse this file and build an object that I can populate a JListBox with the names and a radiobutton group with the different servers, however each project consists of a different amount of servers. How do I iterate the nodes/childnodes to build the object with multiple servers. Here is snippets of the code I’m using borrowed from a website and some from me and I’m not very good at coding yet so bear with me please. When I debug it starts to parse & build the object but once it gets to the server names it prints a null pointer exception so I’m doing something totally wrong.

public class XMLParser {
    public Project currentProject = new Project();

    public void parseXML() throws Exception {

        try {
            File file = new File("c:\\projectlist.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(file);
            doc.getDocumentElement().normalize();

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

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

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

                    currentProject.SetAppName(getTagValue("name", eElement));
                    currentProject.SetType(getTagValue("type", eElement));
                    currentProject.SetEnvironment(getTagValue("environment", eElement));
                    currentProject.SetServerName(getTagValue("server", 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();
          }


public final class Project {
    protected String AppName = null;
    protected String Type = null;
    protected List<String> ServerNames = null;
    protected String Environment = null;

    public void SetAppName(String AppName) {
        this.AppName = AppName;
    }

    public void SetType(String DeployType) {
        this.Type = DeployType;
    }

    public void SetServerName(String ServerName) {
        this.ServerNames.add(ServerName);
    }

    public void SetEnvironment(String Environment) {
        this.Environment = Environment;
    }

    public String getAppName() {
        return AppName;
    }

    public String getType() {
        return Type;
    }

    public List<String> getServerName() {
        return ServerNames;
    }

    public String getEnvironment() {
        return Environment;
    }


}
  • 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-27T09:29:46+00:00Added an answer on May 27, 2026 at 9:29 am

    Your exception is being caused because you didn’t initialize ServerNames in your Project class. Try to initialize it as follows and rerun:

    final protected List<String> ServerNames = new ArrayList<String>();

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

Sidebar

Related Questions

I have an XML file in the following format: <doc> <id name=X> <type name=A>
I have an XML file in a string in this format: <item> <name>xxx</name> <id>yyy</id>
I have a XML file of the format: <outer1> <inner1> <name>Stonecold</name> <profession>warrior</profession> <org>wwf</org> </inner1>
I have an XML file format that contains a structure of questions: <question id=q101>
I have a custom XML file format which can contain blocks of code within
I have an XML based file format that I'm using to store and load
I have a XML file with the following data format: <net NetName=abc attr1=123 attr2=234
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have a datetime coming back from an XML file in the format: 20080916
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have

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.