此代码解析本地存储的 xml 文件,然后从解析的数据创建动态 UI,尽管我还没有完成 UI 部分。直到昨天它工作正常,现在突然没有响应。
public class XML_PARSER extends Activity {
String TAG= "XML_PARSER";
List optionList = new ArrayList(); ;
Document dom;
Document doc;
Menu mymenu=null;
String msg=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parser);
new ParseXML().execute();
}
private class ParseXML extends AsyncTask<Integer, Integer, Document>{
@Override
protected Document doInBackground(Integer... params) {
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
Document dom1=null;
try {
//Uri uri = Uri.parse("android.resource://com.example.xml_parser/raw/options");
InputStream is=getResources().openRawResource(R.raw.options);
DocumentBuilder db = dbf.newDocumentBuilder();
dom1=db.parse(is);
Log.i(TAG,"parsing done");
}
catch(ParserConfigurationException pce){
pce.printStackTrace();
}
catch(SAXException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
return dom1;
}
@Override
public void onPostExecute(Document d) {
ParseDocument(d);
//onCreateOptionsMenu(mymenu);
text();
}
}
@SuppressWarnings("unchecked")
public void ParseDocument(Document dom){
Element docEle = dom.getDocumentElement();
Node node;
NodeList n1= docEle.getElementsByTagName("Option");
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
node=n1.item(i);
Element e1=(Element)n1.item(i);
它以某种方式卡在这个 getOption() 上,因为如果写在下面,则不会打印日志,而如果写在上面,则会打印日志。
Option e = getOption(e1,node);
Log.i(TAG,"Parse Document reached");
optionList.add(e);
}
}
}
private Option getOption(Element el,Node parent){
String name= getTextValue(el,"Name");
String type = el.getAttribute("type");
Option op = new Option(name,type);
fillSubOptions(op, el, parent);
return op;
}
private String getTextValue(Element ele, String tagName){
String textVal=null;
NodeList n1 = ele.getElementsByTagName(tagName);
if(n1!=null && n1.getLength()>0){
Element el= (Element)n1.item(0);
textVal =el.getFirstChild().getNodeValue();
}
return textVal;
}
private void fillSubOptions(Option op, Element el, Node parent){
Element ele;
Node node;
String name = null;
NodeList n1 = el.getElementsByTagName("Option");
int count =0;
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
ele = (Element)n1.item(i);
node= n1.item(i);
if((node.getParentNode()).equals(parent)){
name= getTextValue(ele, "Name");
count= op.printSubOptions(count);
op.setSubOptions(name);
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mymenu=menu;
Iterator<Option> it= optionList.iterator();
int count=0;
while(it.hasNext()){
Option e= (Option)it.next();
if(e.getType().equals("menu")){
count++;
menu.add(0,count,0,e.getName());
}
}
getMenuInflater().inflate(R.menu.parser, menu);
return true;
}
public class Option {
String name;
String type;
String parent;
int count;
ArrayList<String> subOptions;
Option(String name,String type)
{
setName(name);
setType(type);
subOptions = new ArrayList<String>();
parent = null;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setSubOptions(String subOption) {
(this.subOptions).add(subOption);
}
public String getName() {
return name;
}
public int printSubOptions(int count) {
Iterator<String> it = (this.subOptions).iterator();
if(it.hasNext()){
while(it.hasNext()){
count++;
}
}
return count;
}
public String getType() {
return type;
}
}