android xml解析,检查id并读取相应的值

时间:2012-03-23 11:47:48

标签: android xml-parsing

我有一个XML文件,其中包含ID,名称和URL

我需要读取文件并检查ID的值。如果它等于soe参数,我将读取名称和URL并停止检查。否则我会前进到下一个元素并进行相同的检查。

我有以下iPhone代码。它与我需要做的类似,但我需要在Android上执行此操作:

NSString *url = @"my http url to get xml file";    
NSURL *xmlURL = [NSURL URLWithString:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName     
{
    if([elementName isEqualToString:@"person"])
    {
        Pickers.person= person; 
        Pickers.fullName   = fullName;
        Pickers.ID = ID;
        Pickers.URl = URl;
        return;
    }

    //There is nothing to do if we encounter the Books element here.
    // and release the object.

    NSString *P_ID ;
    NSString *P_URl;

    if([elementName isEqualToString:@"ID"]) 
    {
        P_ID = currentElementValue;
        NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\n"];

        P_ID = [[P_ID componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

        P_ID =  [P_ID stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

        [ID addObject:P_ID];
    }    
    else if([elementName isEqualToString:@"Name"]) 
    {

        [fullName addObject:currentElementValue];
    }
    else  if([elementName isEqualToString:@"Symbol"]) 
    { 
        [person addObject:currentElementValue];
    }
}

else if([elementName isEqualToString:@"URl"]) 
{
    P_URl = currentElementValue;
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"\n"];

    P_URl = [[P_URl componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

    P_URl =  [P_URl stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

    [URl addObject:P_URl];
}


}

另外我想知道在我的情况下哪个更好用:SAX解析器,DOM解析器或XMLPullParser?

3 个答案:

答案 0 :(得分:0)

这应该让你开始。

URL url = new URL("your http url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
InputStream in = conn.getInputStream();
Reader reader = new InputStreamReader(new BufferedInputStream(in));
pullParser.setInput(reader);
int event;
String tag;
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
    if ( XmlPullParser.START_TAG == event ) {
        tag = parser.getName();
        if (tag.equals("ID")) {
        //your logic here
        }           
    }
}

答案 1 :(得分:0)

以下是您可能会发现有用的XML解析器示例:

public class XmlParserDemo {

private List<String> titles = new ArrayList<String>();      
public List<String> getTitles() {   //getter    
    return titles;      
}

public void processTag(XmlPullParser xParser, String tagName) throws IOException,
XmlPullParserException {
    if ("TAG".equals(tagName)) {    //xml Tag
        titles.add(xParser.nextText());
    }       
}

public static void main (String args[]) throws XmlPullParserException, IOException{     
    XmlParserDemo xp = new XmlParserDemo(); // create OBJ       
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();             
    URL url = new URL("your http URL");
    InputStreamReader reader = new InputStreamReader(url.openStream());
    xpp.setInput(reader);                   
    int eventType = xpp.getEventType();                                         
    while(eventType != XmlPullParser.END_DOCUMENT){              
        if(eventType == XmlPullParser.START_TAG){
            xp.processTag(xpp, xpp.getName()); 
        }                        
        eventType = xpp.next();
    }           
    List<String> xmlValues = xp.getTitles();    //passing getter values
    StringBuffer sb = new StringBuffer();
    for(String values:xmlValues){
     sb.append("\t");
     sb.append(values);
     sb.append("\n");
    }
    System.out.println("\n" + sb.toString());  //Print Values                                                  
 }  

}

答案 2 :(得分:0)

经过长时间的研究和试验和错误,我最喜欢使用Simple框架,最有效,最简单(代码少,错误少);使用起来非常简单有趣!

你可以找到它here