我想用但拉解析解析本地XML的特定部分,我不知道我是怎么看的部分。 我使用的代码:
package com.example.xmltest;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myXmlContent = (TextView)findViewById(R.id.xml_tv);
String stringXmlContent;
stringXmlContent = getAllXML();
myXmlContent.setText(stringXmlContent);
}
public String getAllXML(){
Activity activity = this;
String str = "";
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.ensaheehint);
try {
xpp.next();
int eventType = xpp.getEventType();
System.out.println("eventType : " + eventType);
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT){
str += "nXML Parsing Starting...n";
}
else if(eventType == XmlPullParser.START_TAG)
{
eventType = xpp.next();
if(eventType == XmlPullParser.TEXT){
String text = xpp.getName();
str += "**TEXT: "+text;
}
}
else if(eventType == XmlPullParser.END_TAG)
{
str += "nending tag: "+xpp.getName();
}
else if(eventType == XmlPullParser.TEXT)
{
str += "nvalue : "+xpp.getText();
}
eventType = xpp.next();
}
str += "nnXML parsing Ending......";
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
下面是我的XML文件看起来是这样的:我试图访问位于在时间绫一个文本字符串。
<quran>
<sura index="1" name="الفاتحة">
<aya index="1" text="In the name of Allah, the Entirely Merciful, the Especially Merciful."/>
<aya index="2" text="[All] praise is [due] to Allah, Lord of the worlds -"/>
<aya index="3" text="The Entirely Merciful, the Especially Merciful,"/>
<aya index="4" text="Sovereign of the Day of Recompense."/>
<aya index="5" text="It is You we worship and You we ask for help."/>
<aya index="6" text="Guide us to the straight path -"/>
<aya index="7" text="The path of those upon whom You have bestowed favor, not of those who have evoked [Your] anger or of those who are astray."/>
</sura>