2012-05-22 107 views
0

我遇到TFHpple並執行基本的XML搜索查詢時遇到問題。我已經包含了代碼和XML響應。我有一個MutableArray存儲返回的數據,然後我NSLog它。在解析HTML頁面之前,以下代碼對我來說工作得很好(當然,我使用了initWIthHTMLData)。任何幫助將不勝感激。TFHpple XML返回NULL

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL  
URLWithString:@"http://api.wmata.com/Rail.svc/Stations?  
LineCode=RD&api_key=---------------------------"]]; 

// Create parser 
TFHpple *xpathParser = [[TFHpple alloc] initWithXMLData:data]; //using XML data 

//Pass in the search path 
NSArray *elements = [xpathParser searchWithXPathQuery:@"//Name"]; 


// Access the first cell 
if([elements count] > 0) { 

    TFHppleElement *element = [elements objectAtIndex:0]; 

    // Get the text within the cell tag 
    NSString *content = [element content]; 

    if (content) { 
     //NSLog(@"%@", content); 
     self.linkElements = [elements copy]; 

    }else{ 
     NSLog(@"No string found in the content"); 
    } 


}else{ 
    NSLog(@"Nothing was found for your search query"); 
} 


NSLog(@"The following is the result of the link elements array: %@", self.linkElements); 

以下是一個示例XML響應:

<StationsResp xmlns="http://www.wmata.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<Stations> 
<Station> 
    <Code>C02</Code> 
    <Lat>38.9013327968</Lat> 
    <LineCode1>BL</LineCode1> 
    <LineCode2>OR</LineCode2> 
    <LineCode3 i:nil="true" /> 
    <LineCode4 i:nil="true" /> 
    <Lon>-77.0336341721</Lon> 
    <Name>McPherson Square</Name> 
    <StationTogether1 /> 
    <StationTogether2 /> 
</Station> 
<Station> 
    <Code>C01</Code> 
    <Lat>38.8983144732</Lat> 
    <LineCode1>BL</LineCode1> 
    <LineCode2>OR</LineCode2> 
    <LineCode3 i:nil="true" /> 
    <LineCode4 i:nil="true" /> 
    <Lon>-77.0280779971</Lon> 
    <Name>Metro Center</Name> 
    <StationTogether1>A01</StationTogether1> 
    <StationTogether2 /> 
</Station> 
<Station> 
    <Code>A01</Code> 
    <Lat>38.8983144732</Lat> 
    <LineCode1>RD</LineCode1> 
    <LineCode2 i:nil="true" /> 
    <LineCode3 i:nil="true" /> 
    <LineCode4 i:nil="true" /> 
    <Lon>-77.0280779971</Lon> 
    <Name>Metro Center</Name> 
    <StationTogether1>C01</StationTogether1> 
    <StationTogether2 /> 
</Station> 
... 

回答

-1

使用此示例XML:

<?xml version="1.0"?> 
<Station> 
    <Code>C02</Code> 
    <Lat>38.9013327968</Lat> 
    <LineCode1>BL</LineCode1> 
    <LineCode2>OR</LineCode2> 
    <LineCode3 i:nil="true" /> 
    <LineCode4 i:nil="true" /> 
    <Lon>-77.0336341721</Lon> 
    <Name>McPherson Square</Name> 
    <StationTogether1 /> 
    <StationTogether2 /> 
</Station> 

而且從[element content][element text]變化的,這是我的輸出:

麥克弗森廣場

看起來你可以使用[element text]Please refer to this similar question/answer

我希望它有幫助。