android的读取xml配置文件,在android中读取xml文件 – 解析

  • Post author:
  • Post category:其他


第一个平局做你自己:

public final class XMLManager {

public static HashMap> getHmPlist(){

HashMap> hmPlist = null;

try {

hmPlist = new HashMap<>();

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(“D:\\Loic_Workspace\\Test2\\res\\test.xml”));

NodeList ndList = doc.getElementsByTagName(“key”);

Integer keyLength = ndList.getLength();

String keyValue = null;

NodeList ndArray = null;

Integer ndArrayLength = null;

ArrayList alElement = null;

for(int i=0;i

alElement = new ArrayList<>();

keyValue = ndList.item(i).getTextContent();

if(keyValue != null){

ndArray = ndList.item(i).getNextSibling().getNextSibling().getChildNodes();

ndArrayLength = ndArray.getLength();

for(int j=0;j

if(ndArray.item(j).getNodeName().equalsIgnoreCase(“string”)){

alElement.add(ndArray.item(j).getFirstChild().getTextContent());

}

}

hmPlist.put(keyValue, alElement);

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SAXException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ParserConfigurationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return hmPlist;

}

}

为了anserw你关于res/raw /文件夹的问题。是的,你必须把你的XML放在该文件夹中,并通过R类在Java代码中使用它。

希望它能帮助,