GDataXMLDocument nodesForXPath返回错误

时间:2013-06-21 10:13:20

标签: iphone ios xpath xml-parsing gdata

我需要你的帮助,我想使用GDataXMLDocument解析一个XML文件,我想获取特定标签的所有属性,但是当我使用或只是提供XPath的值来获取columnHeading值

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:path] encoding:NSUTF8StringEncoding  error:NULL];

NSArray *tempArray = [doc nodesForXPath:@"//root/xsd:schema" namespaces:nil error:nil];

它返回错误以下。

XPath错误:未定义的命名空间前缀 xmlXPathEval:评估失败

这是我的XML内容。

<?xml version="1.0" encoding="utf-8"?>
<SH xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:saw-sql="urn:saw-sql" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset">
    <xsd:complexType name="R">
        <xsd:sequence>
            <xsd:element name="S0" saw-sql:columnHeading="SID" saw-sql:columnID="0"/>
            <xsd:element name="S1" saw-sql:columnHeading="SNAME" saw-sql:columnID="1"/>
            <xsd:element name="S2" saw-sql:columnHeading="CLASS" saw-sql:columnID="2"/>
            <xsd:element name="S3" saw-sql:columnHeading="ADD" saw-sql:columnID="3"/>
            <xsd:element name="S4" saw-sql:columnHeading="CITY" saw-sql:columnID="4"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

最近3天被困在这里。需要你的帮助。

提前致谢。

1 个答案:

答案 0 :(得分:3)

最后我得到了与我的问题相关的解决方案......这就是答案。

GDataXMLDocument * doc = [[GDataXMLDocument alloc] initWithData:[NSData dataWithContentsOfFile:path] encoding:NSUTF8StringEncoding error:NULL];

NSDictionary *myNS = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"urn:schemas-microsoft-com:xml-analysis:rowset", @"xmlns",
                      @"http://www.w3.org/2001/XMLSchema", @"xsd",
                      @"urn:saw-sql",@"saw-sql", nil];

 NSArray *tempArray = [doc nodesForXPath:@"//xmlns:RS/xsd:schema/xsd:complexType/xsd:sequence/xsd:element" namespaces:myNS error:nil];

NSMutableArray *keyArray = [[NSMutableArray alloc]initWithCapacity:0];

for(GDataXMLElement *columns in tempArray)
{
    GDataXMLNode *xmlElement = [columns attributeForName:@"saw-sql:columnHeading"];

    //  GDataXMLNode* xmlElement = [[[element2 elementsForName:@"xsd:element"]objectAtIndex:0]attributeForName:@"saw-sql:columnHeading"];
    [keyArray addObject:xmlElement.stringValue];
    //  NSLog(@"%@",xmlElement.stringValue);

}