Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将XML解析为Tilemap_Java_Xml_Dom - Fatal编程技术网

Java 将XML解析为Tilemap

Java 将XML解析为Tilemap,java,xml,dom,Java,Xml,Dom,我正试图使用JavaDOM将XML文件解析为int[][],但我不知道该怎么做,它总是给我奇怪的结果 以下是XML文档: <layers> <name>Untitled Layer</name> <rows> <cells> <tileCode>1</tileCode> </cells> <cells> <tileCode>2</tileCod

我正试图使用JavaDOM将XML文件解析为int[][],但我不知道该怎么做,它总是给我奇怪的结果

以下是XML文档:

<layers>
<name>Untitled Layer</name>
<rows>
  <cells>
    <tileCode>1</tileCode>
  </cells>
  <cells>
    <tileCode>2</tileCode>
  </cells>
</rows>
<rows>
  <cells>
    <tileCode>-1</tileCode>
  </cells>
  <cells>
    <tileCode>4</tileCode>
  </cells>
</rows>
</layers>
我想从中得到: [1, 2] [-1,4]

但它返回:[0,1,0,2,0] [0,-1,0,4,0]?

你可以这样做

    import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFile {

    public static void main(String argv[]) {

        try {

            File fXmlFile = new File("D:/StackOverFlow/DOMTester/src/you.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            NodeList rowList = doc.getElementsByTagName("rows");
            int[][] map = new int[rowList.item(0).getChildNodes().getLength()-3][rowList.getLength()];
            for (int y = 0; y < rowList.getLength(); ++y) {
                NodeList cellList = rowList.item(y).getChildNodes();
                int k=0;
                for (int x = 0; x < cellList.getLength(); ++x) {
                    Node cell = cellList.item(x);
                                    if (cell.getNodeType() == Node.ELEMENT_NODE) {

                        if((Integer.parseInt(((Element) cell).getElementsByTagName("tileCode").item(0).getTextContent()))!=0){

                        map[k][y] = Integer.parseInt(((Element) cell)
                                .getElementsByTagName("tileCode").item(0)
                                .getTextContent());
                        k++;

                        }
                    }
                }
            }


            for (int y = 0; y < map[0].length; ++y) {
                for (int x = 0; x < map.length; ++x) {
                    System.out.print(map[x][y] + " ");
                }
                System.out.println("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
发出

1 2


-1 4

您是否可以使用ArrayList而不是int[][]并向其添加数据??它将为您提供所需的输出
    import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFile {

    public static void main(String argv[]) {

        try {

            File fXmlFile = new File("D:/StackOverFlow/DOMTester/src/you.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            NodeList rowList = doc.getElementsByTagName("rows");
            int[][] map = new int[rowList.item(0).getChildNodes().getLength()-3][rowList.getLength()];
            for (int y = 0; y < rowList.getLength(); ++y) {
                NodeList cellList = rowList.item(y).getChildNodes();
                int k=0;
                for (int x = 0; x < cellList.getLength(); ++x) {
                    Node cell = cellList.item(x);
                                    if (cell.getNodeType() == Node.ELEMENT_NODE) {

                        if((Integer.parseInt(((Element) cell).getElementsByTagName("tileCode").item(0).getTextContent()))!=0){

                        map[k][y] = Integer.parseInt(((Element) cell)
                                .getElementsByTagName("tileCode").item(0)
                                .getTextContent());
                        k++;

                        }
                    }
                }
            }


            for (int y = 0; y < map[0].length; ++y) {
                for (int x = 0; x < map.length; ++x) {
                    System.out.print(map[x][y] + " ");
                }
                System.out.println("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}