0

Here is an XML file I'm trying to parse with MXML library:

<?xml version="1.0" encoding="utf-8"?>
<msg xmlns="http://uri.etsi.org/ori/002-2/v1.1.1">
    <header>
        <msgType>REQ</msgType>
        <msgUID>4567</msgUID>
    </header>
    <body>
        <updateSwPrepReq>
            <ftpSrvIpAddress>192.168.1.1</ftpSrvIpAddress>
            <ftpSrvUserName>SW_User</ftpSrvUserName>
            <ftpSrvPassword></ftpSrvPassword>
            <ftpSrvSwPkgDirPath>/swImages/acme</ftpSrvSwPkgDirPath>
            <SwUpgradePkgVer>RAN-201204-008</SwUpgradePkgVer>
        </updateSwPrepReq>
    </body>
</msg>

I can parse and get the element name ('msgTyp'e, 'msgUID'), but not the associated values ('REQ', '4567').

Here is a part of my code:

mxml_node_t      *tree = NULL;
mxml_node_t      *node = NULL;
mxml_node_t      *node_tmp = NULL;

tree = mxmlLoadFile(NULL, "Test.xml", MXML_TEXT_CALLBACK);

node = mxmlFindElement(tree, tree, "msg", NULL, NULL, MXML_DESCEND);
tmp = mxmlElementGetAttr(node, "xmlns");
printf("msg attribut : %s \n", tmp);

node_tmp = mxmlFindElement(tree, tree, "msgType", NULL, NULL, MXML_DESCEND);
if (node_tmp != NULL)
{
    printf("node_tmp not null");
    printf("msgType : %s", node_tmp->child->value.text.string);
}

node_tmp is not NULL because the first printf works. But I get a segmentation fault (core dumped) error.

Can anyone help me? Thanks in advance

EDIT : I found the solution (it works in my case...). Hope it can help...

Just open the file with this command:

tree = mxmlLoadFile(NULL, "Test.xml", MXML_OPAQUE_CALLBACK);

And get the value with this one:

printf("msgType : %s", node_tmp->child->value.opaque);
10h02
  • 35
  • 5

3 Answers3

0

To get the element's text you should use

mxmlGetText(node_tmp,0)

instead of node_tmp->child->value.text.string

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
0

try this

if (node_tmp != NULL)
{
    printf("node_tmp not null");
    mxml_node_t *b=mxmlWalkNext(node_tmp, tree, MXML_DESCEND_FIRST);
    printf("msgType : %s", b->value.text.string);
}
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

you should test the type of node, and you should change tree with node in mxmlFindElement it cause problem with tag <?xml version="1.0" encoding="utf-8"?>

mxml_node_t      *tree = NULL;
mxml_node_t      *node = NULL;
mxml_node_t      *node_tmp = NULL;

tree = mxmlLoadFile(NULL, "Test.xml", MXML_TEXT_CALLBACK);

node = mxmlFindElement(tree, tree, "msg", NULL, NULL, MXML_DESCEND);
tmp = mxmlElementGetAttr(node, "xmlns");
printf("msg attribut : %s \n", tmp);

node_tmp = mxmlFindElement(node, node, "msgType", NULL, NULL, MXML_DESCEND);
if (node_tmp != NULL && node_tmp->type == MXML_ELEMENT)
{
    printf("node_tmp not null\n");
    mxml_node_t *b=mxmlWalkNext(node_tmp, node, MXML_DESCEND_FIRST);
    if(b->type == MXML_TEXT)
        printf("msgType : %s\n", b->value.text.string);
}
developer
  • 4,744
  • 7
  • 40
  • 55