2012-05-22 2 views
0

TFHpple에 문제가있어 기본 XML 검색 쿼리를 수행하고 있습니다. 나는 코드와 XML 응답을 포함시켰다. 내가 반환 된 데이터를 저장하고 NSLog 그것을 저장 MutableArray있다. 다음 코드는 HTML 페이지를 파싱 할 때 (물론 initWIthHTMLData :를 사용했다.) 대신에 나에게 잘 돌아갔다. 어떤 도움이라도 대단히 감사하겠습니다.TFHpple XML에서 NULL이 반환됩니다.

<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] 방법이 내 출력은을 다음

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 응답입니다 :

당신이 [element text]를 사용할 수처럼

맥퍼슨 광장

것 같습니다. Please refer to this similar question/answer.

도움이되기를 바랍니다.

관련 문제