我试图从资产文件夹中解析我的 xml 文件,我在 android 2.3.5 上尝试过它可以工作,但是当我在 android 4.0.4 和 4.1 中尝试它时它没有给出任何错误,但我无法从 xml 中获取值.
public class PlistParser {
// parse Plist and fill in arraylist
public ArrayList<DataModel> parsePlist(String xml) {
final ArrayList<DataModel> dataModels = new ArrayList<DataModel>();
//Get the xml string from assets
final Document doc = XMLfromString(xml);
final NodeList nodes_array = doc.getElementsByTagName("dict");
//Fill in the list items from the XML document
for ( int index = 0; index < nodes_array.getLength(); index++ ) {
final Node node = nodes_array.item(index);
if ( node.getNodeType() == Node.ELEMENT_NODE ) {
final Element e = (Element)nodes_array.item(index);
final NodeList nodeKey = e.getElementsByTagName("key");
final NodeList nodeValue = e.getElementsByTagName("string");
DataModel model = new DataModel();
for (int i=0; i < nodeValue.getLength(); i++) {
final Element eleKey = (Element)nodeKey.item(i);
final Element eleString = (Element)nodeValue.item(i);
if ( eleString != null ) {
String strValue = getValue(eleString, "string");
if(getValue(eleKey, "key").equals("isim")) {
model = new DataModel();
model.setIsim(strValue);
} else if(getValue(eleKey, "key").equals("turu")) {
model.setTuru(strValue);
} else if(getValue(eleKey, "key").equals("kalori")) {
model.setKalori(Float.parseFloat(strValue));
} else if(getValue(eleKey, "key").equals("protein")) {
model.setProtein(Float.parseFloat(strValue));
} else if(getValue(eleKey, "key").equals("yag")) {
model.setYag(Float.parseFloat(strValue));
}
else if(getValue(eleKey, "key").equals("karbonhidrat")) {
model.setKarbonhidrat(Float.parseFloat(strValue));
} else if(getValue(eleKey, "key").equals("lif")) {
model.setLif(Float.parseFloat(strValue));
}else if(getValue(eleKey, "key").equals("aciklama")) {
if ( strValue == null ) {
strValue = "";
}
model.setAciklama(strValue);
dataModels.add(model);
}
}
}
}
}
return dataModels;
}
// Create xml document object from XML String
private Document XMLfromString(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
doc.getDocumentElement().normalize();
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
// fetch value from Text Node only
private String getElementValue(Node elem) {
Node kid;
if (elem != null) {
if (elem.hasChildNodes()) {
for (kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
if (kid.getNodeType() == Node.TEXT_NODE) {
return kid.getNodeValue();
}
}
}
}
return "";
}
/// Fetch value from XML Node
private String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return getElementValue(n.item(0));
}
}
这是从资产中获取 xml 的代码。
private String readPlistFromAssets() {
StringBuffer sb = new StringBuffer();
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("sample.xml")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException ex) {
ex.printStackTrace();
}
}
return sb.toString();
}