gpt4 book ai didi

java - Android解析Plist文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:24 28 4
gpt4 key购买 nike

你好,我正在尝试解析一个包含字典数组的 plist 文件。我正在尝试使用 xmlwise 来做到这一点. plistfile 的内容是 here

到目前为止,我的 Activity 中只有这个,我正在获取 plistfile 的内容,但是如何将内容解析为数组列表?

Map<String, Object> properties = null;
try {
InputStream inputStream = getResources().openRawResource(R.raw.first_5);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
properties = Plist.fromXml(sb.toString());
// TODO do something with the object here
Log.v("--", properties.values() + " " + properties.size());
Log.v("--", "OB "+properties.get());
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
}
}

最佳答案

快速提问。 ArrayList 的内容应该是什么?我想知道你是否提到了 Object 的列表在你里面Map<String, Object> properties map 那么为什么你不能从 map 中获取值作为

Map<String, Object> properties = new HashMap<String, Object>();
List<Object> plist = new ArrayList<Object>(properties.values());

除了检查您的 plist 之外,该结构就像一个 Dict 根元素和其中的一个 Dict 列表。我假设您需要将其作为列表获取。如果是这样考虑使用 Android PList parser通过长寿软件。这很简单而且是开源的。它基本上是一个 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<PListObject> 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;
}

希望这对您有所帮助。

关于java - Android解析Plist文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649839/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com