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 に答える