我想解析並從Web服務獲取值。問題是Web服務具有相同的元素名稱標籤,這些元素名稱標籤正在創建問題來解析,因爲我想獲取它們的值,但它們具有相同的名稱。因此,我很難將它們提及爲本地名稱。Android:如何解析SAX解析器中的相同標記?
<countryBean>
<id>236</id>
<name>United State</name>
</countryBean>
<secutiryQuestion1>
<id>2</id>
<question>What was your dream job as a child?</question>
</secutiryQuestion1>
<secutiryQuestion2>
<id>4</id>
<question>What is the name, breed, and color of your pet?</question>
</secutiryQuestion2>
<stateBean>
<country>236</country>
<id>5</id>
<name>California</name>
</stateBean>
<statusBean>
<id>1</id>
<name>Active</name>
</statusBean>
我想獲得ID標籤和其他相鄰標籤的價值,如名稱,問題在不同的變量。這裏有5個Id標籤。
我的類的代碼是這樣
public class XmlParser extends DefaultHandler {
public RestfullResponse tempResponse = null;
String currentValue = null;
int ServiceType =0;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equalsIgnoreCase("countryBean")){
tempResponse=new RestfullResponse();
}
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(ServiceType == 1)
{
if(localName.equalsIgnoreCase("id")){
tempResponse.setCountryId(currentValue);
}
if(localName.equalsIgnoreCase("name")){
tempResponse.setCountryName(currentValue);
}
if(localName.equalsIgnoreCase("id")){
tempResponse.setStateId(currentValue);
}
if(localName.equalsIgnoreCase("question")){
tempResponse.setstateBean(currentValue);
}
if(localName.equalsIgnoreCase("Id")){
tempResponse.setStatusId(currentValue);
}
if(localName.equalsIgnoreCase("requestSuccess")){
tempResponse.setStatus(currentValue);
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue = new String(ch, start, length);
}
public RestfullResponse getResponse(){
return tempResponse;
}
}
請幫忙將它們存儲在
String CountryName= "";
String CountryId = "";
String Statename = "";
String StateId ="";
String Status = "";
String StatusId = "";
String Username ="";
String SecurityQuestion = "";
String SecurityQuestionId = "";
感謝您的解決方案 – vuhung3990 2016-02-26 03:48:02