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

  • Home
  • SEARCH
  • 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 7875915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:04:46+00:00 2026-06-03T03:04:46+00:00

Hi all I have an XML file in java which holds a 2d array

  • 0

Hi all I have an XML file in java which holds a 2d array of numbers it looks something like

<tableNumbers>
     <row id="0">
         <column id="0"> 4 </column>
         <column id="1"> 2 </column>
         <column id="2"> 5 </column>
         <column id="3"> 6 </column>
    </row>
    <row id="1">
         <column id="0"> 5 </column>
         <column id="1"> 10 </column>
         <column id="2"> 7 </column>
         <column id="3"> 9 </column>
    </row>
</tableNumbers>

Now every row in the table has the same number of colums and what I am trying to do is cycle through the xml and store the number from the XML file into an Integer array. (e.g. row 0 column 0 would be stored in numbers[0][0].

The code I currently have is:

public static Integer[][] getNumbers(File file, int noRows, int noColums){
        Integer[][] numbersArray = new Integer[noRows][noColumns];

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        try {
            Document document = docBuilderFactory.newDocumentBuilder().parse(file);
            Element rootElement = document.getDocumentElement(); 

            NodeList rowList = rootElement.getElementsByTagName("row");
            if ((rowList != null)) 
                for (int i = 0; i < rowList.getLength(); i++) {
                    NodeList columnsList = rowList.item(i).getChildNodes();
                    if ((columnsList != null)) 
                        for (int j = 0; j < columnsList.getLength(); j++) {
                            Element number = (Element) columnsList.item(j); 
                            System.out.println("(" +i + "," + j + ") " + number.getNodeValue());
                            numbersArray[i][j] = number.getNodeValue();
                        }
                }

            return numbersArray;
        }
        catch (Exception c){}
        return null;
}

A few lines from the standard the output are:

(0,0) null
(0,1) null
(0,2) null
(0,3) null
(1,0) null
(1,1) null
(1,2) null
(1,3) null

The value which is returned from all the cells is null. I know the error is reading from the xml file. If anyone could show me where I am going wrong I would be most grateful

  • 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-03T03:04:48+00:00Added an answer on June 3, 2026 at 3:04 am

    I would add the following:

    Element number = (Element) columnsList.item(j);
    if ("column".equals(number.getTagName())) }
        System.out.println("(" +i + "," + j + ") " + number.getTextContent());
    }
    

    Since you may have an empty element. Also, I’m using getTextContent() method from Node declaration.

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

Sidebar

Related Questions

I have a Java WebService setup which consumes an xml file and want to
I have defined a local mirror for all repositories in the settings.xml file: <mirror>
I have set up multiple targets in a single xml file. I expect all
I have a map that reads an XML file; it's all very simple and
Do you have some predefined set of targets which all build.xml files you create
So, i have some question about xml Documents in Java. Can i get all
I have tried all that i could to generate the R.java file but in
I have to complete this task: I'm given a xml file which is exported
I have an xml file right now which has a TableLayout within a LinearLayout.
I have a big (1.9 GB) XML file which has data I want 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.