xml - libxml
http://xmlsoft.org/
설치
$ sudo apt install libxml2-dev
sample code
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
static void s_print_attributes(xmlNode * node)
{
xmlAttr * props = node->properties;
xmlNode * attr;
if (props == NULL) {
return;
}
printf("{");
for (; props; props = props->next) {
if (props->prev) {
printf(" ");
}
printf("%s: %s", props->name, props->xmlChildrenNode->content);
}
printf("}");
}
static void s_print_first_child(xmlNode * node, const char * prefix)
{
xmlNode * first = node->xmlChildrenNode;
if (!first || first->next) {
return;
}
printf("%s", prefix);
printf("Content: '%s'\n", first->content);
}
static void s_print_child_elements(xmlNode * node, const char * prefix)
{
xmlNode * child;
for (child = node->xmlChildrenNode; child; child = child->next) {
if (child->type == XML_ELEMENT_NODE) {
char prefix_[10] = {0,};
snprintf(prefix_, sizeof(prefix_), " %s", prefix);
printf("%s", prefix);
printf("Name: %s ", child->name);
s_print_attributes(child);
printf("\n");
s_print_first_child(child, prefix_);
s_print_child_elements(child, prefix_);
}
}
}
int main(int argc, char *argv[])
{
LIBXML_TEST_VERSION
xmlDoc * doc;
xmlNode * root;
xmlNode * cur_node = NULL;
if (argc < 2) {
fprintf(stderr, "usage: %s <xml path>\n", argv[0]);
exit(1);
}
doc = xmlReadFile(argv[1], NULL, 0);
if (doc == NULL) {
fprintf(stderr, "xmlReadFile() failed\n");
exit(1);
}
root = xmlDocGetRootElement(doc);
s_print_child_elements(root, "");
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
return 0;
}
빌드
$ gcc -o parse main.c `pkg-config --cflags --libs libxml-2.0`
light.xml
<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<deviceType>urn:schemas-upnp-org:device:BinaryLight:0.9</deviceType>
<friendlyName>Living Room</friendlyName>
<manufacturer>My company</manufacturer>
<manufacturerURL>URL to manufacturer site</manufacturerURL>
<modelDescription>Remote Light Control</modelDescription>
<modelName>Light</modelName>
<modelNumber>0001</modelNumber>
<modelURL>URL to model site</modelURL>
<serialNumber>SN0001</serialNumber>
<UDN>uuid:UUID</UDN>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:SwitchPower:1</serviceType>
<serviceId>urn:upnp-org:serviceId:SwitchPower:1</serviceId>
<SCPDURL>/switchpower.xml</SCPDURL>
<controlURL>URL for control</controlURL>
<eventSubURL>URL for eventing</eventSubURL>
</service>
</serviceList>
</device>
</root>
실행
$ ./parse light.xml
Name: specVersion
Name: major
Content: '1'
Name: minor
Content: '0'
Name: device
Name: deviceType
Content: 'urn:schemas-upnp-org:device:BinaryLight:0.9'
Name: friendlyName
Content: 'Living Room'
Name: manufacturer
Content: 'My company'
Name: manufacturerURL
Content: 'URL to manufacturer site'
Name: modelDescription
Content: 'Remote Light Control'
Name: modelName
Content: 'Light'
Name: modelNumber
Content: '0001'
Name: modelURL
Content: 'URL to model site'
Name: serialNumber
Content: 'SN0001'
Name: UDN
Content: 'uuid:UUID'
Name: serviceList
Name: service
Name: serviceType
Content: 'urn:schemas-upnp-org:service:SwitchPower:1'
Name: serviceId
Content: 'urn:upnp-org:serviceId:SwitchPower:1'
Name: SCPDURL
Content: '/switchpower.xml'
Name: controlURL
Content: 'URL for control'
Name: eventSubURL
Content: 'URL for eventing'
switchpower.xml
<?xml version="1.0"?>
<scpd xmlns="urn:schemas-upnp-org:service-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<actionList>
<action>
<name>SetTarget</name>
<argumentList>
<argument>
<name>newTargetValue</name>
<relatedStateVariable>Target</relatedStateVariable>
<direction>in</direction>
</argument>
</argumentList>
</action>
<action>
<name>GetTarget</name>
<argumentList>
<argument>
<name>RetTargetValue</name>
<relatedStateVariable>Target</relatedStateVariable>
<direction>out</direction>
</argument>
</argumentList>
</action>
<action>
<name>GetStatus</name>
<argumentList>
<argument>
<name>ResultStatus</name>
<relatedStateVariable>Status</relatedStateVariable>
<direction>out</direction>
</argument>
</argumentList>
</action>
</actionList>
<serviceStateTable>
<stateVariable sendEvents="no">
<name>Target</name>
<dataType>boolean</dataType>
<defaultValue>0</defaultValue>
</stateVariable>
<stateVariable sendEvents="yes">
<name>Status</name>
<dataType>boolean</dataType>
<defaultValue>0</defaultValue>
</stateVariable>
</serviceStateTable>
</scpd>
실행
$ ./parse switchpower.xml
Name: specVersion
Name: major
Content: '1'
Name: minor
Content: '0'
Name: actionList
Name: action
Name: name
Content: 'SetTarget'
Name: argumentList
Name: argument
Name: name
Content: 'newTargetValue'
Name: relatedStateVariable
Content: 'Target'
Name: direction
Content: 'in'
Name: action
Name: name
Content: 'GetTarget'
Name: argumentList
Name: argument
Name: name
Content: 'RetTargetValue'
Name: relatedStateVariable
Content: 'Target'
Name: direction
Content: 'out'
Name: action
Name: name
Content: 'GetStatus'
Name: argumentList
Name: argument
Name: name
Content: 'ResultStatus'
Name: relatedStateVariable
Content: 'Status'
Name: direction
Content: 'out'
Name: serviceStateTable
Name: stateVariable {sendEvents: no}
Name: name
Content: 'Target'
Name: dataType
Content: 'boolean'
Name: defaultValue
Content: '0'
Name: stateVariable {sendEvents: yes}
Name: name
Content: 'Status'
Name: dataType
Content: 'boolean'
Name: defaultValue
Content: '0'
댓글 없음:
댓글 쓰기