MiniXML解析XML的属性值

时间:2017-09-27 18:38:31

标签: c xml xml-parsing

我需要在嵌入式环境中使用紧凑的XML解析器。通常我会简单地使用XSL来提取我需要的数据,但是没有足够的空间。我一直在寻找miniXML作为一种可能的解决方案,但遇到了一个我无法解决的问题。

测试文件将加载到树中没有问题,我可以找到没有问题的节点(元素)。但是,当我尝试获取该元素的属性值时,miniXML返回NULL值。有谁知道什么可能是错的?

#include "stdio.h"

#include "mxml.h"
#include <fcntl.h>

#ifndef O_BINARY
#  define O_BINARY 0
#endif /* !O_BINARY */

int                 /* O - Exit status */
main(int  argc,             /* I - Number of command-line args */
     char *argv[]) {
    FILE *fp;
    mxml_node_t *tree, *node;
    const char *attribute;
    fp = fopen("test2.xml", "r");
    tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
    if (!tree) {
        fputs("Unable to read XML file with default callback.\n", stderr);
        return (1);
    }
    fclose(fp);

    if ((node = mxmlFindPath(tree, "SOAP-ENV:Envelope/SOAP-ENV:Body/g2swsdl:g2sRequest/g2s:g2sMessage/g2s:g2sBody")) != NULL) {
        fputs(" Found g2sBody element in XML tree.\n", stderr);    
    }

    attribute = mxmlElementGetAttr(node, "g2s:egmId");  
    fprintf(stderr, "Attribute Text Value is %s.\n", attribute);
}

这是测试文件:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:g2s="http://www.gamingstandards.com/g2s/schemas/v1.0.3" 
    xmlns:g2swsdl="http://www.gamingstandards.com/wsdl/g2s/v1.0">
<SOAP-ENV:Body>
    <g2swsdl:g2sRequest>
    <g2s:g2sMessage >
     <g2s:g2sBody 
                g2s:dateTimeSent="2017-09-08T01:21:52Z" 
                g2s:egmId="ATI_B0321931" 
                g2s:hostId="1">
     <g2s:communications 
            g2s:timeToLive="30000" 
            g2s:sessionMore="false" 
            g2s:sessionRetry="false" 
            g2s:sessionId="182908" 
            g2s:sessionType="G2S_request" 
            g2s:commandId="1" 
            g2s:dateTime="2017-09-08T01:20:53Z" 
            g2s:deviceId="1">           
        <g2s:commsOnLine 
            g2s:metersReset="false" 
            g2s:subscriptionLost="false"
            g2s:deviceChanged="false"
            g2s:deviceReset="false"
            g2s:egmLocation="http://192.169.200.11 21778">
        </g2s:commsOnLine>
     </g2s:communications>
     </g2s:g2sBody>
     </g2s:g2sMessage>
     </g2swsdl:g2sRequest>
     </SOAP-ENV:Body>
     </SOAP-ENV:Envelope>

0 个答案:

没有答案