Dar*_*ski 8 java android plist saxparser android-xml
您好我试图解析包含dict数组的plist文件.我试图用xmlwise来做这件事.plistfile的内容在这里
到目前为止,我只在我的活动中有这个和我获取plistfile的内容,但如何将内容解析为arraylist?
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();
}
}
Run Code Online (Sandbox Code Playgroud)
快问。ArrayList 的内容应该是什么?Object我想知道您是否提到了地图中的列表Map<String, Object> properties,那么为什么不能从地图中获取值
Map<String, Object> properties = new HashMap<String, Object>();
List<Object> plist = new ArrayList<Object>(properties.values());
Run Code Online (Sandbox Code Playgroud)
除了检查 plist 之外,结构就像一个 Dict 根元素和其中的 Dict 列表。我想你需要把它作为一个列表。如果是这样,请考虑使用longevitysoft 的Android PList 解析器。这很简单并且开源。它基本上是一个解析Apple PList 的SAXParserSAXParser 。
然后您可以迭代该数组并获取适当的对象。在你的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试解析你的 xml 时,我遇到了一些异常。这是因为 PListXMLHandler 正在检查 localName 而不是 QualifiedName。通过检查 startElement() 和 endElement() 等方法中的 localName 可以轻松解决此问题。
if(isEmpty(localName)){
localName = qName;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
10466 次 |
| 最近记录: |