【问题标题】:Parse xml data into hashtable and displaying in listview?将 xml 数据解析为哈希表并在列表视图中显示?
【发布时间】:2011-08-18 06:03:20
【问题描述】:


我有个问题。在从需要插入哈希表并显示在列表视图中的 xml 文件中进行 SAX 解析后,我有多个记录。我能够在列表视图中显示数据,但它提供的只是列表视图中所有行的相同数据。我认为这与我对哈希表的编码或我的 SAX 解析有关。

这是我试图从网上检索数据的 xml 文件:http://spark.opac.tp.edu.sg/X?op=present&set_no=007584&set_entry=000000001,000000002,000000003,000000004,000000005&format=marc

下面是我的处理程序,我将数据放入哈希表中:

ArrayList<Hashtable<String,String>> list = new ArrayList<Hashtable<String,String>>();

Hashtable<String,String> data = new Hashtable<String,String>();

private final String MY_DEBUG_TAG = "Debugging~";

private Boolean in_record = false;
private Boolean in_author = false;
private Boolean in_format = false;
private Boolean in_title = false;
private Boolean in_callNumber = false;

private String format = "";
private String title = "";
private String author = "";
private String callNumber = "";

//private SearchList sList;

//public SearchList getListData() {
//  return this.sList;
//}

@Override
public void startDocument() throws SAXException {
 //       this.sList = new SearchList();
}

@Override
public void endDocument() throws SAXException {
        // Nothing to do
}

/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
 * -- <name> )*/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if(localName.equalsIgnoreCase("record")) {
        this.in_record = true;
    }
    else
        if (localName.equalsIgnoreCase("fixfield")) {
            if (attributes.getValue("id").equalsIgnoreCase("FMT")) {
               this.in_format = true;
            } 
        }
        else
            if(localName.equalsIgnoreCase("varfield")) {
                if(attributes.getValue("id").equalsIgnoreCase("100")) {
                    this.in_author = true;
                }
                else
                    if(attributes.getValue("id").equalsIgnoreCase("245")) {
                        this.in_title = true;
                    }
                    else 
                        if(attributes.getValue("id").equalsIgnoreCase("099")) {
                            this.in_callNumber = true;
                        }
            }
}

public void characters(char[] ch, int start, int length)
throws SAXException {

       StringBuffer sb = new StringBuffer();
       String finalString;
       String originalString = "";

       String chars = new String(ch, start, length); 
       chars = chars.trim(); 

       if (this.in_format) {
           sb.append(chars);
           finalString = sb.toString();
           this.format = finalString;
       }
       else
           if (this.in_author) {
               if(!(chars.equals(""))) {
                   sb.append(chars);
                   finalString = sb.toString();
                   originalString = this.author + finalString;
                   this.author = originalString;
               }
           }
           else
               if (this.in_title) {
                   if(!(chars.equals(""))) {
                       sb.append(chars);
                       finalString = sb.toString();
                       originalString = this.title + finalString;
                       this.title = originalString;
                   }
               }          
               else
                   if (this.in_callNumber) {
                       if(!(chars.equals(""))) {
                           sb.append(chars);
                           finalString = sb.toString();
                           this.callNumber = finalString;
                       }
                   }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

       if (localName.equalsIgnoreCase("record")) {
           this.in_record=false;
       }
       else
           if (localName.equalsIgnoreCase("fixfield")) {
               if (this.in_format) {
                  //set the foundCharacter to Hashtable
                  data.put("format", this.format);
                  //sList.setFormat(this.format);
                  Log.d(MY_DEBUG_TAG,"Format = " + this.format);
                  this.in_format = false;
                } 
           }
           else
               if(localName.equalsIgnoreCase("varfield")) {
                   if (this.in_author) {
                       //set the foundCharacter to Hashtable
                       data.put("author", this.author);
                       //sList.setAuthor(this.author);
                       Log.d(MY_DEBUG_TAG,"Author = " + this.author);
                       this.in_author = false;
                   }
                   else 
                       if (this.in_title) {
                           //set the foundCharacter to Hashtable
                           data.put("title", this.title);
                           //sList.setTitle(this.title);
                           Log.d(MY_DEBUG_TAG,"Title = " + this.title);
                           this.in_title = false;
                       }
                       else 
                           if (this.in_callNumber) {
                               //set the foundCharacter to Hashtable
                               data.put("callNumber", this.callNumber);
                               //sList.setCallNumber(this.callNumber);
                               Log.d(MY_DEBUG_TAG,"Call Number = " + this.callNumber);
                               this.in_callNumber = false;
                           }
               }
       //add the hashtable into ArrayList        
       list.add(data);
}

各位有什么建议吗?

【问题讨论】:

    标签: android xml listview hashtable sax


    【解决方案1】:

    我没有时间完整阅读您的代码...

    但这是一个简单的例子:

    ListView myListView;
    myListView = (ListView) findViewById(R.id.mylistview);
    ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map;
    
    //load your data
    String[][] items = database.getItems("Blog");
    
    //check if the database was empty
    if(items != null){
        for(int i = 0; i < items[0].length; i++) {
            map = new HashMap<String, String>();
            map.put("pubdate", items[2][0]);
            map.put("title", items[0][i]);
            map.put("description", items[1][i]);
            listItem.add(map);
    
        }
    
        //Creation of a SimpleAdapter to put items in your list (listitems) in your listview
        // You need to have a xml file called list_full_news_item with elements
        // called android:id="@+id/title" etc.
        SimpleAdapter mSimpleAdaptor = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list_full_news_item,
            new String[] {"pubdate", "title", "description"}, new int[] {R.id.itemPubdate, R.id.itemTitle, R.id.itemDescription});
       //Assign to the listView the created adapter
       myListView.setAdapter(mSimpleAdaptor);
    }
    

    希望这能帮助你理解。

    【讨论】:

    • 谢谢^^。非常适合我
    【解决方案2】:

    您不应使用 SAX 解析器,而应使用 Simple XML Library。它有@ElementMap annotation 正是为了这个目的。这会将您的整个问题变成一个注释和三行代码来读取 XML。Look at my blog post 将库包含在 android 项目中,look at the Simple tutorial 如果您想了解如何使用它的所有酷功能。

    编辑:我刚刚意识到我之前已经在您的previous questions 中回答过这个问题。我已经尽力说服您研究 Simple XML;但是请不要在这里以五种不同的方式发布本质上完全相同的问题;人们已经帮助了你,给你答案,并引导你走向正确的方向。现在是阅读更多内容、实验和学习的时候了。

    【讨论】:

      猜你喜欢
      • 2010-12-26
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 2015-12-15
      相关资源
      最近更新 更多