2011-09-06 173 views
5

我試着用KissXML解析下面的XML文件(與KISSxml iphone生成):kissXML返回的XPath查詢沒有值

<?xml version="1.0" encoding="UTF-8"?> 
<SnowProfile xmlns="http://caaml.org/Schemas/V5.0/Profiles/SnowProfileIACS" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://caaml.org/Schemas/V5.0/Profiles/SnowProfileIACS http://caaml.org/Schemas/V5.0/Profiles/SnowprofileIACS/CAAMLv5_SnowProfileIACS.xsd" gml:id="2011-09-04-Home"> 
    <metaDataProperty> 
    <MetaData> 
     <dateTimeReport>2011-09-04T18:45:10-0700</dateTimeReport> 
     <srcRef> 
     <Operation> 
      <name>My op</name> 
      <contactPerson> 
      <Person> 
       <name>Jesse</name> 
      </Person> 
      </contactPerson> 
     </Operation> 
     </srcRef> 
    </MetaData> 
    </metaDataProperty> 
    <validTime> 
    <TimeInstant> 
     <timePosition>2011-09-04T18:45:10-0700</timePosition> 
    </TimeInstant> 
    </validTime> 
    <snowProfileResultsOf> 
    <SnowProfileMeasurements dir="top down"> 
     <comment>Testing</comment> 
     <profileDepth uom="cm">58</profileDepth> 
     <skyCond>BKN</skyCond> 
     <precipTI>None</precipTI> 
     <airTempPres uom="degC">2</airTempPres> 
     <windSpd uom="ms-1">5</windSpd> 
     <windDir> 
     <AspectPosition> 
      <position>E</position> 
     </AspectPosition> 
     </windDir> 
     <hS> 
     <Components> 
      <snowHeight uom="cm">3</snowHeight> 
     </Components> 
     </hS> 
     <penetrationSki uom="cm">14</penetrationSki> 
     <penetrationFoot uom="cm">8</penetrationFoot> 
     <penetrationRam uom="cm">22</penetrationRam> 
     <stratProfile> 
     <Layer> 
      <depthTop uom="cm">0</depthTop> 
      <thickness uom="cm">27</thickness> 
      <grainFormPrimary>PPsd</grainFormPrimary> 
      <grainSize uom="mm">14</grainSize> 
      <hardness uom="">4F</hardness> 
      <lwc uom="">W</lwc> 
      <density uom="kg/m3">310</density> 
      <validFormationTime> 
      <timeInstant> 
       <timePosition>2011-09-05</timePosition> 
      </timeInstant> 
      </validFormationTime> 
     </Layer> 
     <Layer> 
      <depthTop uom="cm">27</depthTop> 
      <thickness uom="cm">31</thickness> 
      <grainFormPrimary>PPnd</grainFormPrimary> 
      <grainFormSecondary>PPpl</grainFormSecondary> 
      <grainSize uom="mm">11</grainSize> 
      <hardness uom="">1F</hardness> 
      <lwc uom="">S</lwc> 
      <density uom="kg/m3">263</density> 
      <validFormationTime> 
      <timeInstant> 
       <timePosition>2011-07-05</timePosition> 
      </timeInstant> 
      </validFormationTime> 
     </Layer> 
     </stratProfile> 
     <tempProfile uomDepth="cm" uomTemp="C"> 
     <Obs> 
      <depth>2</depth> 
      <snowTemp>0</snowTemp> 
     </Obs> 
     <Obs> 
      <depth>18</depth> 
      <snowTemp>-4</snowTemp> 
     </Obs> 
     <Obs> 
      <depth>52</depth> 
      <snowTemp>-1</snowTemp> 
     </Obs> 
     </tempProfile> 
     <stbTests> 
     <RBlockTest> 
      <comment>Test</comment> 
      <failedOn> 
      <Layer uom="cm"> 
       <depthTop>25</depthTop> 
      </Layer> 
      <Results> 
       <testScore>3</testScore> 
       <fractureCharacter>Q3</fractureCharacter> 
      </Results> 
      </failedOn> 
     </RBlockTest> 
     </stbTests> 
    </SnowProfileMeasurements> 
    </snowProfileResultsOf> 
    <locRef> 
    <ObsPoint> 
     <name>Home</name> 
     <validElevation> 
     <ElevationPosition uom="m"> 
      <position>110</position> 
     </ElevationPosition> 
     </validElevation> 
     <validAspect> 
     <AspectPosition> 
      <position>SW</position> 
     </AspectPosition> 
     </validAspect> 
     <validSlopeAngle> 
     <SlopeAnglePosition uom="deg"> 
      <position>2</position> 
     </SlopeAnglePosition> 
     </validSlopeAngle> 
     <pointLocation> 
     <gml:Point srsDimension="2"> 
      <gml:pos></gml:pos> 
     </gml:Point> 
     </pointLocation> 
    </ObsPoint> 
    </locRef> 
</SnowProfile> 

使用下面的代碼從它創建一個核心數據對象:

-(BOOL)importFile:(NSString *)fileName error:(NSError **)error{ 
    NSData *data = [[NSData alloc] initWithContentsOfFile:fileName]; 
    if(data == nil) 
     return NO; 
    DDXMLDocument *doc = [[DDXMLDocument alloc] initWithData:data options:0 error:error]; 
    //if(error != nil) 
    if(doc == nil) 
     return NO; 

    snowPit *pit = [snowPit pitForCAAMLProfile:doc]; 
    if(pit != nil) 
     return YES; 
    else 
     return NO; 
} 

+(snowPit *)pitForCAAMLProfile:(DDXMLDocument *)xmlDoc{ 
    NSLog(@"%@", [xmlDoc XMLStringWithOptions:DDXMLNodePrettyPrint]); 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; 
    NSError *err; 

    snowPit *newPit = [[pitManager sharedManager] newPitWithoutDefaults]; 
    NSArray *fields = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CAAMLImportFields" ofType:@"plist"]]; 
    for(NSDictionary *importField in fields){ 
     NSString *field = [importField valueForKey:@"field"]; 
     NSString *xpath = [importField valueForKey:@"xpath"]; 
     NSString *attribute = [importField valueForKey:@"attribute"]; 
     NSArray *nodes; 
     if(xpath != nil && [xpath length] > 0){ 
      nodes = [xmlDoc nodesForXPath:xpath error:nil]; 
      NSLog(@"Field:%@, xpath:%@, attribute:%@ nodes:%i", field, xpath, attribute, [nodes count]); 
      if(nodes != nil && [nodes count] > 0){ 
       NSString *value; 
       if(attribute != nil && [attribute length] > 0){ 
        DDXMLNode *attNode = [[nodes objectAtIndex:0] attributeForName:attribute]; 
        if(attNode != nil) 
         value = [attNode stringValue]; 
       }else 
        value = [[nodes objectAtIndex:0] stringValue]; 
       if(value != nil && [value length] > 0) 
        [newPit setValue:value forKey:field]; 
      } 
     } 
    } 
    [dateFormatter release]; 
    return newPit; 
} 

但到目前爲止,我嘗試的每個XPath查詢都會返回一個長度爲0的數組。

要獲得名稱的值,我試過//Person/namePerson/name//namename而且它們都返回0長度的數組。即使查詢/SnowProfile也會返回0長度數組,這導致我認爲xml解析出現問題。我已經驗證了XML文件,並且很好,並且從NSlog輸出中我可以看出該文件正在被加載。

回答

3

有定義xmlns:gml="http://www.opengis.net/gml"

從我的XPath的回憶命名空間,你需要指定,當你做一個XPath查詢。嘗試/gml:SnowProfile以查看是否返回0長度數組以外的內容。然後你會知道這是否能解決你的問題。

+2

錯誤竟然是默認命名空間xmlns =「http://caaml.org/Schemas/V5.0/Profiles/SnowProfileIACS」,使其正確解析我已經加載文件作爲mutableString並在將其放入DDXmlDocument之前擺脫名稱空間聲明。 – Jesse

0

您必須用「noNSxml」替換html標記的「xmlns」屬性以具有正確的解析。如果你想重用它,它建議你然後把buck xmlns。

0

不是將文件作爲文本處理並修改它,可以重命名默認命名空間並進行查詢。詳情請見this answer,附代碼。