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

ParseXml()

Parse the following XML document.

<request_message>
  <issuer>SALES0001</issuer>
  <ship_order date='2021/09/10' customerID='C00001' customerName='A corporation'>
    <orders>
      <order productID='PID0000100' name='A product'/>
      <order productID='PID0000200' name='B product'/>
    </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">
        <orders>
            <order productID="PID0000100" name="A product"/>
            <order productID="PID0000200" name="B product"/>
        </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' customerID='C00001' customerName='A corporation'>"
"    <orders>"
"      <order productID='PID0000100' name='A product'/>"
"      <order productID='PID0000200' name='B product'/>"
"    </orders>"
"  </ship_order>"
"</request_message>";

void ParseXml(void)
{
    XML_HANDLE hXml;
    int ErrCode;
    XML_Node *ship_order;
    XML_Attribute *customerName;
    XML_Node *order;
    XML_Attribute *productID;
    XML_Attribute *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));

    ship_order = XML_FindNodeByPath(hXml, "ship_order", &ErrCode);
    if (ship_order){
        customerName = XML_FindAttribute(hXml, ship_order, "customerName", &ErrCode);
        if (customerName && customerName->value){
            printf("%s\n", customerName->value);
        }
    }

    order = XML_FindNodeByPath(hXml, "ship_order/orders/order", &ErrCode);
    if (order){
        while (order){
            productID = XML_FindAttribute(hXml, order, "productID", &ErrCode);
            if (!productID || !productID->value){
                printf("error\n");
                break;
            }
            name = XML_FindAttribute(hXml, order, "name", &ErrCode);
            if (!name || !name->value){
                printf("error\n");
                break;
            }
            printf("productID=%s\n", productID->value);
            printf("name=%s\n", name->value);
            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>"
"    <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 *ship_notification;
    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;
    }

    ship_notification = XML_FindNodeByPath(hXml,"ship_notification", &ErrCode);
    if (ship_notification){
        if (!XML_SetAttribute(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_SetAttribute(hXml, order, "productID", shipped[i][0], &ErrCode)){
                printf("error=%d\n", ErrCode);
                break;
            }
            if (!XML_SetAttribute(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\"?>", "RESULT2.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">
        <orders>
            <order productID="PID0000100" name="A product"/>
            <order productID="PID0000200" name="B product"/>
        </orders>
    </ship_notification>
    <comment>&lt;Done!&gt;</comment>
</response_message>

*/
}

Last updated: 2021/10/20