将String []添加到ArrayList是否有错误?

时间:2014-04-10 11:14:23

标签: java xml arrays string parsing

我有以下XML文件。尽可能地,有2个单独的文件:

 <doc>
<str name="id">1</str>
<str name="full_message_t">"""“God loves you more daddy....God loves them more</str>
<str name="source_t">” tweets teenage daughter of #MH370 chief steward http://t.co/Abw96As7xv"""</str>
<str name="news_t">TODAY</str>
<str name="link_t">"""“God loves you more daddy....God loves them more</str>
<long name="_version_">1464787951004155905</long>
</doc>

<doc>
<str name="id">2</str>
<str name="full_message_t">"""Flight #MH370 ended flight in southern Indian Ocean</str>
<str name="source_t"> says @NajibRazak http://t.co/NfesnDrcsP"""</str>
<str name="news_t">TOMORROW</str>
<str name="link_t">"""Flight #MH370 ended flight in southern Indian Ocean</str>
<long name="_version_">1464787951005204483</long>
</doc>

以下SAXParser代码:

public class ReadXMLFile {

    public static void main(String[] args) {
        //Declaration
          final String row[] = new String[5];         //id, full_message_t, source_t, news_t, link_t
          final ArrayList<String[]> tableResult = new ArrayList<String[]>();    //Store ArrayList of row[]

          try {

              SAXParserFactory factory = SAXParserFactory.newInstance();
              SAXParser saxParser = factory.newSAXParser();

              DefaultHandler handler = new DefaultHandler() 
              {
                  //When these are TRUE, extract the ocntent
                    boolean b_id = false;
                    boolean b_full_message_t = false;
                    boolean b_source_t = false;
                    boolean b_news_t = false;
                    boolean b_link_t = false;                                       
                    //############################################################################################
                    //Look for <start> tags
                    public void startElement(String uri, String localName,String qName, 
                            Attributes attributes) throws SAXException {

                            System.out.println("Start Element :" + qName);

                            //Only interested in <str>
                            if (qName.equalsIgnoreCase("str")) {
                                String name = attributes.getValue("name");

                                    //Check for name="id"
                                if(name.equalsIgnoreCase("id")){
                                    b_id = true;
                                }                                
                                    //Check for name="full_message_t"
                                else if(name.equalsIgnoreCase("full_message_t")){
                                    b_full_message_t = true;
                                }
                                else if(name.equalsIgnoreCase("source_t")){
                                    b_source_t = true;
                                }
                                else if(name.equalsIgnoreCase("news_t")){
                                    b_news_t= true;
                                }
                                else if(name.equalsIgnoreCase("link_t")){
                                    b_link_t = true;
                                }                             
                            }//end if

                    }//end startElement

                    //############################################################################################
                    //Look for <end> tags
                    public void endElement(String uri, String localName,
                            String qName) throws SAXException {

                            System.out.println("End Element :" + qName);

                            //When reach </doc>, row Array is complete. Add into ArrayList
                            if (qName.equalsIgnoreCase("doc")) {
                                    System.out.println("Push row into Arraylist : " + row[0]);
                                    tableResult.add(row);
                            }
                    }//end endElement                        
                    //############################################################################################
                    //Get the content
                    public void characters(char ch[], int start, int length) throws SAXException {

                            if (b_id) {
                                    //System.out.println("id : " + new String(ch, start, length));
                                    row[0] = new String(ch, start, length);
                                    System.out.println("The id is " + row[0]);
                                    b_id = false;
                            }
                            else if (b_full_message_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[1] = new String(ch, start, length);
                                    System.out.println("The fullmsg is " + row[1]);
                                    b_full_message_t = false;
                            }
                            else if (b_source_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[2] = new String(ch, start, length);
                                    System.out.println("The source is " + row[2]);
                                    b_source_t = false;
                            }
                            else if (b_news_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[3] = new String(ch, start, length);
                                    System.out.println("The news is " + row[3]);
                                    b_news_t = false;
                            }
                            else if (b_link_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[4] = new String(ch, start, length);
                                    System.out.println("The link is " + row[4]);
                                    b_link_t = false;
                            }

                    }//end characters

           };//end DefaultHandler
                               //############################################################################################
              //Read the String  
              //saxParser.parse("solrTest.xml", handler);
              File file = new File("solrTest.xml");
              InputStream inputStream= new FileInputStream(file);
              Reader reader = new InputStreamReader(inputStream,"UTF-8");

              InputSource is = new InputSource(reader);
              is.setEncoding("UTF-8");

              saxParser.parse(is, handler);

           } catch (Exception e) {
             e.printStackTrace();
           }

          //Test output Arraylist
          System.out.println("Test Result");
          System.out.println(tableResult.get(0));
          System.out.println(tableResult.get(1));

    }
}

在执行结束时,应该有2个String Arrays,它们将被添加到ArrayList中。但是,在打印出地址的最后一行,地址是相同的。 换句话说,最后一个文档存储在ArrayList 两次中。 为什么会这样?第一个医生怎么了?

1 个答案:

答案 0 :(得分:2)

您必须在调用

后重新初始化行数组
tableResult.add(row);
row = new String[5];

否则它始终具有相同的参考。这意味着您的列表有两个条目,引用同一个数组。