nodesForXPath 不返回任何节点:TouchXML

发布于 2024-11-03 00:22:32 字数 2044 浏览 2 评论 0原文

我正在使用 TouchXML 使用 Web 服务,但无法按照教程读取节点。我确实收到了 CXMLDocument 元素,但是,我的nodesForXPath 调用始终返回NULL。可能出什么问题了?

XML 解析代码

NSMutableArray *res = [[NSMutableArray alloc] init];
NSData* valueData=[value dataUsingEncoding:NSUTF8StringEncoding];

    // Do something with the CXMLDocument* result
    CXMLDocument *result = [[[CXMLDocument alloc] initWithData:valueData options:0 error:nil] autorelease];

    NSArray *nodes = NULL;


    nodes = [result nodesForXPath:@"//Outputs" error:nil];

    NSLog (@"Nodes is %@\n", nodes);

    for (CXMLElement *node in nodes) {
        NSLog (@"Test");
        NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
        int counter;
        NSLog (@"Child Count is %@\n",[node childCount]);
        NSLog (@"Child node is%@\n",[[node childAtIndex:0] stringValue]);
        for(counter = 0; counter <= [node childCount]; counter++) {
            //  common procedure: dictionary with keys/values from XML node
            [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];


        }


        //[item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"];  

        [res addObject:item];
        [item release];
    }

    //  and we print our results
    NSLog(@"%@", res);
    [res release];


}

XML:

    <Nodes>
    <Inputs>
        <name>TestProgram</name>
        </Inputs>
        <Outputs>
            <ReturnCode>0</ReturnCode>
            <ReturnMessage>Success</ReturnMessage>
            <NameDetails attr1="value1">Test Program</NameDetails>
            <NameInstance attr1="value1">
            <NameCharacteristics>
                <Dob>04-25-2011</Dob>
            </NameCharacteristics>
            </NameInstance>
        </Outputs>
</Nodes>

nodesForXPatch for //Outputs 始终返回 NULL。我想获取节点值。我尝试用 Dob 替换输出,但仍然得到一个空集。

I'm consuming a Web Service using TouchXML and I'm unable to read the nodes as per the tutorial. I do receive the CXMLDocument element, however, my nodesForXPath call always returns NULL. What could be wrong?

XML Parsing code

NSMutableArray *res = [[NSMutableArray alloc] init];
NSData* valueData=[value dataUsingEncoding:NSUTF8StringEncoding];

    // Do something with the CXMLDocument* result
    CXMLDocument *result = [[[CXMLDocument alloc] initWithData:valueData options:0 error:nil] autorelease];

    NSArray *nodes = NULL;


    nodes = [result nodesForXPath:@"//Outputs" error:nil];

    NSLog (@"Nodes is %@\n", nodes);

    for (CXMLElement *node in nodes) {
        NSLog (@"Test");
        NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
        int counter;
        NSLog (@"Child Count is %@\n",[node childCount]);
        NSLog (@"Child node is%@\n",[[node childAtIndex:0] stringValue]);
        for(counter = 0; counter <= [node childCount]; counter++) {
            //  common procedure: dictionary with keys/values from XML node
            [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];


        }


        //[item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"];  

        [res addObject:item];
        [item release];
    }

    //  and we print our results
    NSLog(@"%@", res);
    [res release];


}

XML:

    <Nodes>
    <Inputs>
        <name>TestProgram</name>
        </Inputs>
        <Outputs>
            <ReturnCode>0</ReturnCode>
            <ReturnMessage>Success</ReturnMessage>
            <NameDetails attr1="value1">Test Program</NameDetails>
            <NameInstance attr1="value1">
            <NameCharacteristics>
                <Dob>04-25-2011</Dob>
            </NameCharacteristics>
            </NameInstance>
        </Outputs>
</Nodes>

nodesForXPatch for //Outputs always returns NULL. I want to fetch the node value. I tried replacing Outputs with Dob and still get an empty set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文