Reading java xml file

Hi all, I have an XML file in java that contains 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 each row in the table has the same number of columns, and what I'm trying to do is loop through xml and save the number from the XML file into an Integer array. (for example, column 0 of row 0 will be stored in the numbers [0] [0].

The code I have now 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 of standard output:

(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 returned from all cells is equal null. I know that the error is being read from an XML file. If anyone could show me what I'm wrong, I would be very grateful

+3
source share
5 answers

I would add the following:

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

. , getTextContent() Node.

+3

XML ? , "row" . </row> (.. ).

0

Try System.out.println("(" +i + "," + j + ") " + number.item(0).getNodeValue());insteadSystem.out.println("(" +i + "," + j + ") " + number.getNodeValue());

OR

NodeList valList = number.getChildNodes();
System.out.println("Last Name : " + ((Node) valList.item(0)).getNodeValue());

Hope this works,

0
source

GetNodeValue returns the value of node - the nodes of the column have no value (except, perhaps, spaces). They should contain text elements, but they will contain the values ​​you need.

0
source

I made some changes to make it work. Here is a snippet.

                    for (int j = 0; j < columnsList.getLength(); j++) {
                        if ("column".equals(columnsList.item(j)
                                .getNodeName())) {
                            Element number = (Element) columnsList.item(j);
                            System.out.println("(" + i + "," + j + ") "
                                    + columnsList.item(j).getTextContent());
                            numbersArray[i][j] = Integer
                                    .parseInt(columnsList.item(j)
                                            .getTextContent().trim());
                        }
                    }
0
source

All Articles