2015-09-28 43 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);