我有xml文件,我需要读取加载级别。此文件位于assets文件夹中。 当我在desctop机器上运行时,一切都很好。但在Android设备上,我尝试解析此文件时会收到FileNotFoundException。 这是阅读方法的代码
public void readLevels() {
this.levelList = new ArrayList<Level>();
FileHandle handle = Gdx.files.internal("lvl/levels.xml");
try {
File fXmlFile = handle.file();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
try {
Document document = documentBuilder.parse(fXmlFile);
document.getDocumentElement().normalize();
NodeList nodeList = document.getElementsByTagName("level");
Level level;
for (int iterator = 0; iterator < nodeList.getLength(); iterator++) {
Node node = nodeList.item(iterator);
Element element = (Element) node;
String levelName = element.getAttribute("name");
int levelNumber = Integer.parseInt(element.getAttribute("order"));
NodeList ballList = element.getElementsByTagName("ball");
List<Ball> levelBallList = new ArrayList<Ball>();
Ball ball;
for (int iter = 0; iter < ballList.getLength(); iter++) {
Element ballIn = (Element) ballList.item(iter);
NodeList positionX = ballIn.getElementsByTagName("position-x");
NodeList positionY = ballIn.getElementsByTagName("position-y");
int x = Integer.parseInt(positionX.item(0).getChildNodes().item(0).getNodeValue());
int y = Integer.parseInt(positionY.item(0).getChildNodes().item(0).getNodeValue());
ball = new Ball(new Vector2(y, x), ballTextures[generateImage()]);
levelBallList.add(ball);
}
level = new Level(levelName, levelNumber, levelBallList);
this.levelList.add(level);
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}