我试图使用KissXML解析以下XML文件(使用KISSxml在iphone上生成):
<?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/name
Person/name
//name
name
并且它们都返回0长度数组。即使是/SnowProfile
的查询也会返回一个0长度的数组,这使我认为xml解析出了问题。我已经验证了XML文件,它没问题,从NSlog输出我可以看出文件正在加载。
答案 0 :(得分:3)
定义了名称空间xmlns:gml="http://www.opengis.net/gml"
从我对xpath的回忆中,你需要在执行xpath查询时指定。尝试/gml:SnowProfile
以查看是否返回0长度数组以外的其他内容。然后你会知道这是否能解决你的问题。
答案 1 :(得分:0)
您必须使用“noNSxml”替换html标记的“xmlns”属性才能进行正确的解析。如果你想重复使用它,那么建议你再使用buck xmlns。
答案 2 :(得分:0)
不是将文件作为文本处理并修改它,而是可以重命名默认命名空间并进行查询。详细信息位于this answer,包含代码。