快问. ArrayList的内容应该是什么?我想知道你是否在提到Map中的Object列表< String,Object>属性映射然后为什么你不能从地图中获取值
Map properties = new HashMap();
List plist = new ArrayList(properties.values());
除了检查你的plist之外,结构就像一个Dict根元素和一个Dicts列表.我假设您需要将此作为列表.如果是这样,请考虑使用Android PList parser by longevitysoft.这很简单,开源.它基本上是SAXParser解析Apple PList.
然后,您可以遍历此数组并获取适当的对象.在你的xml中它和Dict对象的数组,所以你可以做这样的事情
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.longevitysoft.android.xml.plist.PListXMLHandler;
import com.longevitysoft.android.xml.plist.PListXMLParser;
import com.longevitysoft.android.xml.plist.domain.Array;
import com.longevitysoft.android.xml.plist.domain.Dict;
import com.longevitysoft.android.xml.plist.domain.PList;
import com.longevitysoft.android.xml.plist.domain.PListObject;
public class PlistReader {
public static void main(String[] args) throws Exception {
PListXMLParser parser = new PListXMLParser();
PListXMLHandler handler = new PListXMLHandler();
parser.setHandler(handler);
parser.parse(readFile("plist.xml"));
PList pList = ((PListXMLHandler) parser.getHandler()).getPlist();
Dict root = (Dict) pList.getRootElement();
// This Array class is java.util.ArrayList underneath the
// covers
Array theList = root.getConfigurationArray("Objects");
for (PListObject obj : theList) {
switch (obj.getType()) {
case DICT:
Dict dictionaryObj = (Dict) obj;
// dictionaryObj.getConfigurationObject(key);
// dictionaryObj.getConfigurationInteger(key);
// dictionaryObj.getConfiguration(key);
// dictionaryObj.getConfigurationArray(key)
break;
case STRING:
com.longevitysoft.android.xml.plist.domain.String stringObj = (com.longevitysoft.android.xml.plist.domain.String) obj;
break;
default:
break;
}
}
}
private static String readFile(String path) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded);
}
}
当我尝试解析你的xml我有一些例外.那是因为PListXMLHandler正在检查localName而不是qualifiedName.通过检查startElement()和endElement()方法中的localName可以很容易地解决这个问题.
if(isEmpty(localName)){
localName = qName;
}
希望这可以帮助.