将xml数据解析成hashtable并显示在listview中?

发布于 2024-11-05 03:01:46 字数 5832 浏览 3 评论 0原文


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

这是我试图从网上检索数据的xml文件: http://spark.opac.tp.edu.sg/X?op=present&set_no=007584&set_entry=000000001,000000002,000000003,00000 0004, 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);
}

大家有什么建议吗?

I have a problem. I have multiple records that after SAX parsing from a xml file that needs to be inserted into the Hashtable and displayed in a listview. I am able to display the data in the listview but all it gives is the same data for all rows in the listview. I think it has something to do with either my coding for the hashtable or my SAX parsing.

This is the xml file I'm trying to retrieve data from online: http://spark.opac.tp.edu.sg/X?op=present&set_no=007584&set_entry=000000001,000000002,000000003,000000004,000000005&format=marc

Below is my handler where i place my data into the hashtable:

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);
}

Any suggestions guys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-11-12 03:01:46

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

但这是一个简单的例子:

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);
}

希望这会帮助你理解。

I don't have time to read completly your code ...

But this is a simple example :

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);
}

Hope this will help you to understand.

毅然前行 2024-11-12 03:01:46

您不应使用 SAX 解析器,而应使用 简单 XML 库。它具有 @ElementMap 注释正是为了这个目的。这会将整个问题转化为一个注释和三行代码来读取 XML。 查看我的博客文章,了解如何在 Android 项目中包含该库以及 如果您想了解如何使用其所有很酷的功能,请查看简单教程

编辑:我刚刚意识到我之前在您的一个之前的问题。我已经不再试图说服您研究简单 XML;我已经尝试说服您了。但是请停止在这里以五种不同的方式发布本质上完全相同的问题;人们已经帮助了你,给了你答案并引导你走向正确的方向。现在是时候阅读更多、尝试和学习了。

You should not use the SAX Parser and use the Simple XML Library instead. It has the @ElementMap annotation for exactly this purpose. This turns your entire problem into one annotation and three lines of code to read the XML in. Look at my blog post on including the library in an android project and look at the Simple tutorial if you want to see how to use all of its cool features.

Edit: I just realised that I have answered this question before in one of your previous questions. I'm done trying to convince you to look into Simple XML; but please, stop posting essentially the exact same question in five different ways here; people have helped you already, give you answers and lead you in the right direction. It is now time to read more, experiment and learn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文