2016-05-19 31 views
0

我需要使用sax解析器來獲取android中的多個按鈕名稱並將其保存在文件中,並且在某些情況下,如果我做了應該設置(加載)到按鈕文本的任何更改。 Forexample:使用sax作爲getter和setter

Button1文本是:生活是好的; Button2文字是:生活不好

「Button1,生活很好; Button2,生活不好」應該保存在一個log.txt中。 如果我做這樣的log.txt的文件進行任何更改(可以進行更新):「Button1的,生活相當不錯; Button2的,生活不惠康」,比

Button1的文字是:生活相當不錯; Button2的文字是:生活並不順利

那麼,我該怎麼做才行,謝謝。

+0

對於這樣一個小的XML,你爲什麼使用薩克斯?如何dom,jdom,dom4j或vtd-xml? –

+0

這只是樣品原來的一個更大。 – boy

+0

你的檔案有多大? –

回答

0

有一個更簡單的方法來做到這一點:您可以使用字符串資源。 String Resources | Android Developers

<resources> 
    <string name="life_is_x">Life is %s</string> 
    <string name="good">good</string> 
    <string name="not_good">not good</string> 
    <string name="quite_good">quite good</string> 
</resources> 

在你的佈局XML,你並不需要把對屬性android:text值在您的按鈕(儘管如果指定tools:text值將在Android Studio圖形佈局,而不是在設計師的展示應用程序,這是一個設計非常有用)

然後在你的代碼:

Context context = button.getContext(); 
    String lifeStr; 
    switch (lifeEnum) { 
    case GOOD: 
     lifeStr = context.getString(R.string.good); 
     break; 
    case NOT_GOOD: 
     lifeStr = context.getString(R.string.not_good); 
     break; 
    case QUITE_GOOD: 
     lifeStr = context.getString(R.string.quite_good); 
     break; 
    } 
    String title = context.getString(R.string.life_is_x, lifeStr); 
    // lifeStr will replace the %s placeholder in life_is_x 
    button.setText(title); 
+0

謝謝你的回答,但是,我正在使用資源文件。問題是我需要製作程序來記錄上一段文字,並且可以對文字進行更改,對此有何想法? – boy

+0

您的意思是在設備上的Android應用程序中,還是在開發過程中的獨立Java程序中? –

+0

我的意思是一個Android設備。所以,我認爲sax xml setter到文件可能會幫助我存儲帶有按鈕ID的最後一個文本,並且在任何情況下,如程序重新啓動,如果存在,可以加載按鈕文本。任何線索如何? – boy

0

我已經解決了我用JDOM解析器的問題,其實我用這個網站:http://www.tutorialspoint.com/java_xml/java_jdom_parse_document.htm 此鏈接創建xml文件:http://www.tutorialspoint.com/java_xml/java_dom_create_document.htm。謝謝你給我的想法。我推薦任何想在本地Android設備上使用xml操作的人都應該看看http://www.tutorialspoint.com/java_xml/。你可能會發現任何一種xml的例子..

<?xml version="1.0"?> 
<class> 
    <student rollno="393"> 
     <firstname>dinkar</firstname> 
     <lastname>kad</lastname> 
     <nickname>dinkar</nickname> 
     <marks>85</marks> 
    </student> 
    <student rollno="493"> 
     <firstname>Vaneet</firstname> 
     <lastname>Gupta</lastname> 
     <nickname>vinni</nickname> 
     <marks>95</marks> 
    </student> 
    <student rollno="593"> 
     <firstname>jasvir</firstname> 
     <lastname>singn</lastname> 
     <nickname>jazz</nickname> 
     <marks>90</marks> 
    </student> 
</class> 


import java.io.File; 
import java.io.IOException; 
import java.util.List; 

import org.jdom2.Attribute; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.JDOMException; 
import org.jdom2.input.SAXBuilder; 

public class JDomParserDemo { 
    public static void main(String[] args) { 
     try { 
     File inputFile = new File("input.txt"); 

     SAXBuilder saxBuilder = new SAXBuilder(); 

     Document document = saxBuilder.build(inputFile); 

     System.out.println("Root element :" 
      + document.getRootElement().getName()); 

     Element classElement = document.getRootElement(); 

     List<Element> studentList = classElement.getChildren(); 
     System.out.println("----------------------------"); 

     for (int temp = 0; temp < studentList.size(); temp++) {  
      Element student = studentList.get(temp); 
      System.out.println("\nCurrent Element :" 
       + student.getName()); 
      Attribute attribute = student.getAttribute("rollno"); 
      System.out.println("Student roll no : " 
       + attribute.getValue()); 
      System.out.println("First Name : " + student.getChild("firstname").getText()); 
      System.out.println("Last Name : "+ student.getChild("lastname").getText()); 
      System.out.println("Nick Name : "+ student.getChild("nickname").getText()); 
      System.out.println("Marks : "+ student.getChild("marks").getText());       
     } 
     }catch(JDOMException e){ 
     e.printStackTrace(); 
     }catch(IOException ioe){ 
     ioe.printStackTrace(); 
     } 
    } 
}