我正在尝试使用 XMLpullparser 解析本地 xml 文件,但无法正确获取标签名称。
我的xml是
<?xml version="1.0"?>
-
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" >
-
<s:Header>
<Action
xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"
s:mustUnderstand="1" >
http://tempuri.org/IUDTServices/ValidateUser
</Action>
</s:Header>
-
<s:Body>
-
<ValidateUser xmlns="http://tempuri.org/" >
<userName>admin</userName>
<password>admin</password>
</ValidateUser>
</s:Body>
</s:Envelope>
我的活动类包含解析代码,实体类为 UserClass,其中包含用户名和密码参数。
我的活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
InputStream in_s = getApplicationContext().getAssets().open("temp.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);
parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException {
UserClass userClass = null;
int eventType = parser.getEventType();
while(eventType!= XmlPullParser.END_DOCUMENT){
String tagName = null;
switch(eventType){
case XmlPullParser.START_DOCUMENT:
userClass = new UserClass();
case XmlPullParser.START_TAG:
tagName = parser.getName();
if(tagName=="userName"){
userClass.setUsername(tagName);
}
if(tagName=="password"){
userClass.setPassword(tagName);
}
break;
case XmlPullParser.END_DOCUMENT:
printValues(userClass);
break;
}
}
}
private void printValues(UserClass userClass) {
System.out.println(userClass.getUsername()+" "+userClass.getPassword());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我总是将 tagName 设为 null..Could'nt 找到它的原因。请帮助...