0

我在使用 TFHpple 和执行基本的 XML 搜索查询时遇到问题。我已经包含了代码和 XML 响应。我有一个 MutableArray 存储返回的数据,然后我 NSLog 它。在解析 HTML 页面之前,以下代码对我来说工作得很好(当然我使用了 initWIthHTMLData: 代替)。任何帮助将不胜感激。

 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>
...

4

1 回答 1

-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]. 请参考这个类似的问题/答案

我希望它有所帮助。

于 2012-11-28T15:45:34.207 回答