我想解析XMLpullparser的本地xml文件,但無法獲取標記名稱。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的其中包含的用戶名和密碼的parameteres解析代碼。
我的活動
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;
}
}
我收到標記名空always..Could'nt找到it.Plz幫助的原因...
使用'.equals'比較字符串,而不是''==不 – Raghunandan
工作還是...... – user1901079
我EVENTTYPE = parser.getEventType將返回0 .. – user1901079