Java 在listview android中同时解析两个不同的XML文件
我在android项目中研究xml,我是一个初学者。我也有一个不同语言的相同xml,就像那样Java 在listview android中同时解析两个不同的XML文件,java,xml-parsing,xmlpullparser,Java,Xml Parsing,Xmlpullparser,我在android项目中研究xml,我是一个初学者。我也有一个不同语言的相同xml,就像那样 <sura index="1" name="الفاتحة"> <aya index="1" text="الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ" /> <aya index="2" text="الرَّحْمَ
<sura index="1" name="الفاتحة">
<aya index="1" text="الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ
الرَّحِيمِ" />
<aya index="2" text="الرَّحْمَنِ الرَّحِيمِ" />
<aya index="3" text="مَالِكِ يَوْمِ الدِّينِ" />
<aya index="4" text="إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ" />
<aya index="5" text="اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ" />
<aya index="6" text="صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ" />
</sura>
这是阿拉伯语的
<sura index="1" name="الفاتحة">
<aya index="1" text="In the name of Allah, most benevolent, ever-merciful."/>
<aya index="2" text="ALL PRAISE BE to Allah, Lord of all the worlds,"/>
<aya index="3" text="Most beneficent, ever-merciful,"/>
<aya index="4" text="King of the Day of Judgement."/>
<aya index="5" text="You alone we worship, and to You alone turn for help."/>
<aya index="6" text="Guide us (O Lord) to the path that is straight,"/>
<aya index="7" text="The path of those You have blessed, Not of those who have earned Your anger, nor those who have gone astray."/>
</sura>
这是用英语写的
我已经用这段代码成功地用阿拉伯语解析了aya
private List<String> getAllAyaFromSuraIndex(String suraIndex) throws XmlPullParserException, IOException {
list = new ArrayList<>();
XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("sura")) {
String index = parser.getAttributeValue(0);
// Match given sure index
if (index.equals(suraIndex)) {
// Move to first aya tag inside matched sura index
parser.next();
// This loop will exit when it reaches sura end tag </sura>
while (parser.getName().equals("aya")) {
if (parser.getEventType() == XmlPullParser.START_TAG) {
list.add(parser.getAttributeValue(null,"index") + ".\n" + parser.getAttributeValue(null,"text"));
}
// Move to next aya tag
parser.next();
}
break;
}
}
parser.next();
}
return list;
}
private List getAllAyaFromSuraIndex(字符串suraIndex)抛出XmlPullParserException,IOException{
列表=新的ArrayList();
XmlPullParser parser=getResources().getXml(R.xml.quran_阿拉伯语);
while(parser.getEventType()!=XmlPullParser.END_文档){
if(parser.getEventType()==XmlPullParser.START_标记和parser.getName().equals(“sura”)){
字符串索引=parser.getAttributeValue(0);
//匹配给定的确定索引
if(索引等于(suraIndex)){
//移动到匹配的sura索引中的第一个aya标记
parser.next();
//此循环将在到达结束标记时退出
while(parser.getName().equals(“aya”)){
if(parser.getEventType()==XmlPullParser.START_标记){
add(parser.getAttributeValue(null,“index”)+“\n”+parser.getAttributeValue(null,“text”);
}
//移动到下一个aya标签
parser.next();
}
打破
}
}
parser.next();
}
退货清单;
}
我有一个列表视图,有两个文本视图,一个是阿拉伯语,一个是英语。我尝试了所有方法,但没有得到结果。当你开发多语言应用程序时,在android中处理翻译的最佳方法是在locale类别中定义内容。让我给你解释一下 第一步 使用提到的区域名称创建目录值-(语言)例如值ar
MyProject/
res/
values-es/
strings.xml
values-ar/
strings.xml
值es/strings.xml
<resources>
<string name="ayah1">ALL PRAISE BE to Allah, Lord of all the worlds,.</string>
<string name="ayah2">Most beneficent, ever-merciful,</string>
</resources>
<resources>
<string name="ayah1">الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ</string>
<string name="ayah2">الرَّحْمَنِ الرَّحِيمِ</string>
</resources>
使用2个不同的XML进行相关Im。上面的示例使用标记处理xml,我有两个相同的xml,只是语言不同,文本相同。我想问的是,如何一次为同一个标记使用两个不同的XML@尼勒什拉托多,我很抱歉,但我认为你误解了我。我说的是在我的应用程序中显示数据的xml解析。我有两种语言的完整古兰经的2个XML。当用户单击特定的surah时,将打开特定surah的所有Ayat,它们将使用两种语言,即阿拉伯语和英语。我想使用XMLPullParser来解析这两个XML,以在列表视图中显示。在我上面的文章中,我成功地解析了阿拉伯语,但当时我如何也解析第二个xml呢?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.javapapers.multilingualapp.MainActivity">
<TextView
android:id="@+id/string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ayah1" //here the values being used
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
public void setLocale(String localeName) {
if (!localeName.equals(currentLanguage)) {
myLocale = new Locale(localeName);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
refresh.putExtra(currentLang, localeName);
startActivity(refresh);
} else {
Toast.makeText(MainActivity.this, "Language already selected!", Toast.LENGTH_SHORT).show();
}
}