Unicode C
Unicode C
Find XML Element where Attribute = Value
See more XML Examples
Finds an XML element at a particular location where an attribute has a specified value.Chilkat Unicode C Downloads
#include <C_CkXmlW.h>
void ChilkatSample(void)
{
BOOL success;
HCkXmlW xml;
const wchar_t *notUsed;
success = FALSE;
// We have the following XML and wish to find the value of the time attribute where msg = "alert-from".
// <cdr id="4e5d3e7f41bf6201ad81000c29703270" e164="6207">
// <user>
// <grp name="wfln" mode="active"/>
// </user>
// <event msg="setup-to" time="287069" e164="5034"/>
// <event msg="alert-from" time="287069" e164="5034"/>
// <event msg="rel-to" time="287079" e164="5034"/>
// </cdr>
// First, build the above XML:
xml = CkXmlW_Create();
CkXmlW_putTag(xml,L"cdr");
CkXmlW_AddAttribute(xml,L"id",L"4e5d3e7f41bf6201ad81000c29703270");
CkXmlW_AddAttribute(xml,L"e164",L"6207");
CkXmlW_UpdateAttrAt(xml,L"user|grp",TRUE,L"name",L"wfln");
CkXmlW_UpdateAttrAt(xml,L"user|grp",TRUE,L"mode",L"active");
CkXmlW_UpdateAttrAt(xml,L"event",TRUE,L"msg",L"setup-to");
CkXmlW_UpdateAttrAt(xml,L"event",TRUE,L"time",L"287069");
CkXmlW_UpdateAttrAt(xml,L"event",TRUE,L"e164",L"5034");
CkXmlW_UpdateAttrAt(xml,L"event[1]",TRUE,L"msg",L"alert-from");
CkXmlW_UpdateAttrAt(xml,L"event[1]",TRUE,L"time",L"287069");
CkXmlW_UpdateAttrAt(xml,L"event[1]",TRUE,L"e164",L"5034");
CkXmlW_UpdateAttrAt(xml,L"event[2]",TRUE,L"msg",L"rel-to");
CkXmlW_UpdateAttrAt(xml,L"event[2]",TRUE,L"time",L"287079");
CkXmlW_UpdateAttrAt(xml,L"event[2]",TRUE,L"e164",L"5034");
// Show that we have the above XML
wprintf(L"%s\n",CkXmlW_getXml(xml));
// Find the element where msg = "alert-from"
// This updates the xml object's reference to the found element (if successful).
notUsed = CkXmlW_chilkatPath(xml,L"/A/event,msg,alert-from|$");
if (CkXmlW_getLastMethodSuccess(xml) == FALSE) {
wprintf(L"Not found.\n");
CkXmlW_Dispose(xml);
return;
}
// Get the value of the "time" attribute.
wprintf(L"time = %s\n",CkXmlW_getAttrValue(xml,L"time"));
// Restore the xml object's internal reference to the root of the XML document
CkXmlW_GetRoot2(xml);
CkXmlW_Dispose(xml);
}