A sample program that uses the XML library to manipulate nodes in an XML document.

ParseXml()

Parse the following XML document.

<request_message>
  <issuer>SALES0001</issuer>
  <ship_order>
    <date>2021/09/10</date>
    <customer>
      <customerID>C00001</customerID>
      <name>A corporation</name>
    </customer>
    <orders>
      <order>
        <productID>PID0000100</productID>
        <name>A product</name>
      </order>
      <order>
        <productID>PID0000200</productID>
        <name>B product</name>
      </order>
    </orders>
  </ship_order>
</request_message>

EditXml()

Generate the following XML document.

<?xml version="1.0" encoding="UTF-8"?>
<response_message>
    <issuer>foo</issuer>
    <ship_notification>
        <date>2021/09/15</date>
        <orders>
            <order>
                <productID>PID0000100</productID>
                <name>A product</name>
            </order>
            <order>
                <productID>PID0000200</productID>
                <name>B product</name>
            </order>
        </orders>
    </ship_notification>
    <comment>&lt;Done!&gt;</comment>
</response_message>


Sample: main.c
#include <stdio.h>
#include "lib.h"
#include "XML.h"
#include "CodeConversion.h"
#include "logapi.h"

void ParseXml(void);
void EditXml(void);

void main(void)
{
    Cursor(AUTOWRAP);

    while(1){
        printf("1:Parse XML\n");
        printf("2:Edit XML\n");

        while(1){
            if (kbhit()){
                int key = getchar();
                if (key == '1'){
                    ParseXml();
                    break;
                }
                if (key == '2'){
                    EditXml();
                    break;
                }
            }
            Idle();
        }
    }
}

const char *XmlDocument =
"<request_message>"
"  <issuer>SALES0001</issuer>"
"  <ship_order>"
"    <date>2021/09/10</date>"
"    <customer>"
"      <customerID>C00001</customerID>"
"      <name>A corporation</name>"
"    </customer>"
"    <orders>"
"      <order>"
"        <productID>PID0000100</productID>"
"        <name>A product</name>"
"      </order>"
"      <order>"
"        <productID>PID0000200</productID>"
"        <name>B product</name>"
"      </order>"
"    </orders>"
"  </ship_order>"
"</request_message>";

void ParseXml(void)
{
    XML_HANDLE hXml;
    int ErrCode;
    XML_Node *order;
    char *customer;
    char *productID;
    char *name;

    hXml = XML_CreateXmlObject(XML_LOCAL_STRING, XmlDocument, 0, -1, NULL, NULL, &ErrCode);
    if (!hXml){
        printf("error=%d\n", ErrCode);
        return;
    }
    printf("%s\n", XML_GetRootNodeName(hXml, NULL));

    customer = XML_GetNodeValueByPath(hXml, "ship_order/customer/name", &ErrCode);
    if (customer){
        printf("%s\n", customer);
    }

    order = XML_FindNodeByPath(hXml, "ship_order/orders/order", &ErrCode);
    if (order){
        while (order){
            productID = XML_GetChildNodeValueByPath(hXml, order, "productID", &ErrCode);
            name = XML_GetChildNodeValueByPath(hXml, order, "name", &ErrCode);
            if (!productID || !name){
                printf("error\n");
                break;
            }
            printf("productID=%s\n", productID);
            printf("name=%s\n", name);
            order = XML_NextNode(order, "order", &ErrCode);
        }
    }else{
        printf("No order\n");
    }

    XML_ReleaseXmlObject(hXml, NULL);
    return;

/* Output

request_message
A corporation
productID=PID0000100
name=A product
productID=PID0000200
name=B product

*/
}

const char *XmlTemplate =
"<response_message>"
"  <issuer/>"
"  <ship_notification>"
"    <date/>"
"    <orders/>"
"  </ship_notification>"
"</response_message>";

const char *shipped[][2] = {
        {"PID0000100", "A product"},
        {"PID0000200", "B product"},
        {NULL}
};
void EditXml(void)
{
    XML_HANDLE hXml;
    int ErrCode;
    XML_Node *orders;
    XML_Node *order;
    int i;

    hXml = XML_CreateXmlObject(XML_LOCAL_STRING, XmlTemplate, 0, -1, NULL, NULL, &ErrCode);
    if (!hXml){
        printf("error=%d\n", ErrCode);
        return;
    }

    if (!XML_SetNodeValueByPath(hXml, "issuer", "foo", &ErrCode)){
        goto Exit;
    }
    if (!XML_SetNodeValueByPath(hXml, "ship_notification/date", "2021/09/15", &ErrCode)){
        goto Exit;
    }
    orders = XML_FindNodeByPath(hXml, "ship_notification/orders", &ErrCode);
    if (orders){
        for (i = 0; shipped[i][0]; i++){
            order = XML_AddChildNode(hXml, orders, "order", NULL, &ErrCode);
            if (!XML_SetChildNodeValueByPath(hXml, order, "productID", shipped[i][0], &ErrCode)){
                printf("error=%d\n", ErrCode);
                break;
            }
            if (!XML_SetChildNodeValueByPath(hXml, order, "name", shipped[i][1], &ErrCode)){
                printf("error=%d\n", ErrCode);
                break;
            }
        }
        if (ErrCode){
            goto Exit;
        }
        if (!XML_SetNodeValueByPath(hXml, "comment", "<Done!>", NULL)){
            goto Exit;
        }

        if (XML_SaveAsXmlFile(hXml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "RESULT.XML", true, &ErrCode)){
            printf("Saved successfully\n");
        }else{
            printf("error=%d\n", ErrCode);
        }
    }

Exit:
    XML_ReleaseXmlObject(hXml, NULL);
    return;

/* Output

<?xml version="1.0" encoding="UTF-8"?>
<response_message>
    <issuer>foo</issuer>
    <ship_notification>
        <date>2021/09/15</date>
        <orders>
            <order>
                <productID>PID0000100</productID>
                <name>A product</name>
            </order>
            <order>
                <productID>PID0000200</productID>
                <name>B product</name>
            </order>
        </orders>
    </ship_notification>
    <comment>&lt;Done!&gt;</comment>
</response_message>

*/
}

Last updated: 2021/09/24