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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:36:20+00:00 2026-05-27T08:36:20+00:00

I have this project I’m working on where I want to parse an xml

  • 0

I have this project I’m working on where I want to parse an xml file that looks like this:

<?xml version='1.0' encoding='UTF-8'?>
<projectlist>
  <project>
    <name>SuperDuperApp</name>
    <type>batch</type>
    <prod>
      <server>testserver01</server>
    </prod>
    <qa>
      <server>testserver01</server>
    </qa>
    <dev>
      <server>testserver01</server>
    </dev>
  </project>
  <project>
    <name>Calculator</name>
    <type>deploy</type>
    <prod>
      <server>testserver02</server>
      <server>testserver03</server>
      <server>testserver04</server>
    </prod>
    <qa>
      <server>testserver05</server>
      <server>testserver06</server>
      <server>testserver07</server>
    </qa>
    <dev>
      <server>testserver12</server>
      <server>testserver13</server>
      <server>testserver14</server>
    </dev>
  </project>
</projectlist>

With this method parsing the file and trying to print out in the format:

name: SuperDuperApp
type: batch
server: testserver01

name: Calculator
type: deploy
environment: dev
server: testserver12
server: testserver13
server: testserver14

etc.
public void parseXML() {
     ArrayList al = new ArrayList();
     HashSet hs = new HashSet();
    try {
        InputStream file = this.getClass().getResourceAsStream(
                "/net/swing/sandbox/util/config/projectlist.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        doc.getDocumentElement().normalize();

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

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

        System.out.println("Information of all servers...");

        for (int i=0;i<nList.getLength();i++){
            Node fstNode = nList.item(i);

            if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                Element fstElement = (Element) fstNode;

                NodeList nameElementList = fstElement.getElementsByTagName("name");
                Element nameElement = (Element) nameElementList.item(0);
                NodeList name = nameElement.getChildNodes();
                System.out.println("project name: " + ((Node) name.item(0)).getNodeValue());
                hs.add(((Node) name.item(0)).getNodeValue());

                NodeList typeElementList = fstElement.getElementsByTagName("type");
                Element typeElement = (Element) typeElementList.item(0);
                NodeList type = typeElement.getChildNodes();
                System.out.println("Deploy type: " + ((Node) type.item(0)).getNodeValue());

                //print out server list can't do it for some reason         
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


     try {
     al.clear();
     al.addAll(hs);
     Collections.sort(al);
     for (int z = 0; z < al.size(); z++) {
     listModel.addElement(al.get(z));
     }
     } catch (Exception e) {
     e.printStackTrace();
     }
     lstProject.validate();

}

So I rewrote my method and now I’m just stuck <—newb

  • 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-27T08:36:21+00:00Added an answer on May 27, 2026 at 8:36 am

    Check the documentation for Node. Each node has a method getChildNodes. Check that for the existence of children nodes and than iterate over them like you are doing.

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

Sidebar

Related Questions

I have this tiny Qt project with a project file like this: TEMPLATE =
I have this project that I'm working on for a class, and I'm not
I have this project i'm working on and id like to add a really
I have this project that it's due in a few hours and I still
I have this large C++ project that I need to build on a platform
I have a project with a structure like this: project code art config Art
I have this project that i need to add a translation to. I already
So, I have this project that creates multiple instances of a class, and list
I have this new project I need to build. I want to have at
i have this project where i would like to merge a function object (in

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.