2015-09-28 39 views
3

我試圖顯示,與增強功能,文本從一個XML文件中對標籤內得到。以下是打開標籤,文本和結束標記:設定大膽一些文本的話,從XML

<英語> <強>英語形容詞:一名年輕女子< /強> </BR > 他們總是在性別和數量不變。 < /英語>但是,我看到的不僅僅是上述文字,還有<英文>和< /英文>之間的標籤,<強>。如何解決這個問題?由於

這裏是讀取XML file`

public void lireXML(String mot) 
     throws XmlPullParserException, IOException 
{ 

    final TextView monMessage = (TextView) findViewById(R.id.zone_trado_scrollable); 

    XmlResourceParser monFichierXML = getResources().getXml(R.xml.text); 

    monFichierXML.next(); 

    int eventType = monFichierXML.getEventType(); 

    while (eventType != XmlPullParser.END_DOCUMENT) 
    { 
     if((eventType == XmlPullParser.START_TAG) && (monFichierXML.getName().equalsIgnoreCase("word")) ) 
     { 
      monFichierXML.next(); 
      if (monFichierXML.getText().equalsIgnoreCase(mot)) 
      { 

       monFichierXML.nextTag(); // word fermant 
       monFichierXML.nextTag(); // english ouvrant 
       monFichierXML.next(); // texte anglais 

       if (langueChoisie == "francais") { 
        monFichierXML.nextTag(); // english fermant 
        monFichierXML.nextTag(); // français ouvrant 
        monFichierXML.next(); // texte français 
       } 

       if (langueChoisie == "espanol") { 
        monFichierXML.nextTag(); // english fermant 
        monFichierXML.nextTag(); // français ouvrant 
        monFichierXML.next(); // texte français 
        monFichierXML.nextTag(); // français fermant 
        monFichierXML.nextTag(); // espanol ouvrant 
        monFichierXML.next(); // texte espanol 
       } 

       if (langueChoisie == "chinois") { 
        monFichierXML.nextTag(); // english fermant 
        monFichierXML.nextTag(); // français ouvrant 
        monFichierXML.next(); // texte français 
        monFichierXML.nextTag(); // français fermant 
        monFichierXML.nextTag(); // espanol ouvrant 
        monFichierXML.next(); // texte espanol 
        monFichierXML.nextTag(); // espagnol fermant 
        monFichierXML.nextTag(); // russe ouvrant 
        monFichierXML.next(); // texte russe 
        monFichierXML.nextTag(); // russe fermant 
        monFichierXML.nextTag(); // chinois ouvrant 
        monFichierXML.next(); // texte chinois 
       } 

       // on affiche le texte à l'intérieur de la balise sélectionnée 
       monMessage.setText(monFichierXML.getText()); 
      } 
     } 
     eventType = monFichierXML.next(); 
    } 
}` 

的方法和這裏有點xml`的

<group> 
    <word>young1</word> 
    <english><strong>English adjectives: a young woman</strong> <br>They are always invariable in gender and number.</english> 
    <francais><strong>Les adjectifs: a young woman</strong> <br>Les adjectifs qualificatifs anglais sont invariables. Quand ils sont épithètes, ils se placent avant le substantif: a young woman. Quand ils sont attributs, ils se placent après le verbe: the woman is young.</francais> 
    <espanol><strong>Los adjetivos: a young woman</strong> <br>Los adjetivos en inglés son invariables. Cuando son calificativos, se colocan delante del sustantivo: a young woman. Cuando son atributos, se colocan después del verbo: the woman is young.</espanol> 
    <chinois><strong>英語形容詞:一位年輕的女士。</strong> <br>不論性別和數量都一樣</chinois> 
</group>` 
+0

怎麼樣你試圖顯示? –

+0

我用的方法,但我不能表現出來的:-( – Andy

+0

此評論您可以更新您的問題,並粘貼您的方法。 –

回答

3

如果你想使用HTML標籤有一個TextView,你應該使用:

monMessage.setText(Html.fromHtml(monFichierXML.getText())); 
+0

在我看來,這將是一個完全不同的體系結構,它仍然可能仍然閱讀XML? – Andy

+0

XML是不直接相關的,你只是把HTML中Html.fromHtml(的html')。 不要緊,你來自哪裏得到的HTML。 –

+0

完美的作品! – Andy

0

嘗試以下操作:

<b>English adjectives: a young woman</b> <br>They are always invariable in gender and number. 

如果這失敗只是編碼<字符作爲&lt;,導致:

&lt;b>English adjectives: a young woman&lt;/b> <br>They are always invariable in gender and number. 

檢查這個答案詳細信息Bold words in a string of strings.xml in Android

+0

不幸的是,這並不工作(是它<強>或< ...) – Andy

+0

你是如何顯示字符串? –

2

嘗試這樣的事情

TextView myTextview; 

myTextview= (TextView) findViewById(R.id.my_text_view); 

htmltext = <your html (markup) character>; 

Spanned sp = Html.fromHtml(htmltext); 

myTextview.setText(sp);