I started out using Expat, but I didn't like they way the API works - you
write callback functions to handle the tags.  This type of coding can get
pretty messy and hard to debug.

I ended up using ezxml and I'm very happy with it.  It just has one header
file and one .c file that you compile/link with.  I wrote a helper module to
go with it that you may find helpful.  Just include ezxml_ex.h and link to
ezxml_ex.c and ezxml.c

Farrell

On Thu, Mar 19, 2009 at 11:26 AM, Allen Yang <[email protected]>wrote:

> Hi,
>
> I am using an ARM9 based microprocessor. I'd like to use XML file to
> store some configuration data in NAND flash. JFFS2 file system is
> currently used in my system.
>
> Basically I'd like to read configuration data in the XML file, change
> some data and then stored back to the XML file in the NAND flash.
>
> The operation is like reading/writing an EEPROM. Since there is no EPROM
> on my board and XML is more user-friendly, I'd like to try XML.
>
> I did some searches on the internet and found 2 libraries: "Expat" and
> "libxml2". Anybody has idea which one is better to meet my requirement?
>
> Thanks in advance,
>
> Allen
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> _______________________________________________
> uClinux-dev mailing list
> [email protected]
> http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
> This message was resent by [email protected]
> To unsubscribe see:
> http://mailman.uclinux.org/mailman/options/uclinux-dev
>
/* ezxml_ex.c - extensions for ezxml
 * Copyright (C) 2007 Syntech Systems Inc., Farrell J. Aultman <[email protected]>
 */

/* Copyright 2004-2006 Aaron Voisine <[email protected]>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "ezxml_ex.h"

/************************************ get ************************************/

int ezxml_get_str(ezxml_t parent, const char *element, char *value, int size)
{
	ezxml_t child;

	if ((child = ezxml_child(parent, element)) == NULL) return -EBADMSG;  /* bad message */
	strncpy(value, child->txt, size);
	return 0;
}

int ezxml_get_int(ezxml_t parent, const char *element, int *value)
{
	int ret;
	char str[32];

	if ((ret = ezxml_get_str(parent, element, str, sizeof(str))) == 0) *value = atoi(str);
	return ret;
}

int ezxml_get_byte(ezxml_t parent, const char *element, unsigned char *value, int base)
{
	int ret;
	char str[32];

	if ((ret = ezxml_get_str(parent, element, str, sizeof(str))) == 0) {
		*value = (unsigned char)strtol(str, NULL, base);
	}
	return ret;
}

int ezxml_get_bool(ezxml_t parent, const char *element, /*bool*/unsigned char *value)
{
	int ret;
	char str[32];

	if ((ret = ezxml_get_str(parent, element, str, sizeof(str))) == 0) {
		char c = str[0];
		*value = 0;
		if (c == 'Y' || c == 'y' || c == 'T' || c == 't' || c == '1') *value = 1;
	}
	return ret;
}

int ezxml_get_double(ezxml_t parent, const char *element, double *value)
{
	int ret;
	char str[32];

	if ((ret = ezxml_get_str(parent, element, str, sizeof(str))) == 0) *value = atof(str);
	return ret;
}

/************************************ set ************************************/

/* allocates memory for the txt, unlike ezxml_set_txt */
ezxml_t ezxml_set_txt_ex(ezxml_t xml, const char *txt)
{
	if (!xml) return NULL;
	xml->txt = (xml->flags & EZXML_TXTM)
				? realloc(xml->txt, strlen(txt) + 1)
				: malloc(strlen(txt) + 1);
	if (!xml->txt) return NULL;
	ezxml_set_flag(xml, EZXML_TXTM);
	strcpy(xml->txt, txt);
	return xml;
}

int ezxml_set_str(ezxml_t parent, const char *element, const char *value)
{
	ezxml_t child;

	if ((child = ezxml_child(parent, element)) == NULL) return -EBADMSG;  /* bad message */
	if ((ezxml_set_txt_ex(child, value)) == NULL)       return -EINVAL;  /* Invalid argument */
	return 0;
}

int ezxml_set_int(ezxml_t parent, const char *element, int value)
{
	char str[16];

	snprintf(str, sizeof(str), "%d", value);
	return ezxml_set_str(parent, element, str);
}

int ezxml_set_bool(ezxml_t parent, const char *element, unsigned char value)
{
	return ezxml_set_str(parent, element, value ? "True": "False");
}

int ezxml_set_double(ezxml_t parent, const char *element, double value)
{
	char str[16];

	snprintf(str, sizeof(str), "%.5lf", value);
	return ezxml_set_str(parent, element, str);
}

/*********************************** write ***********************************/

/* atomically writes string of xml data to a file */
int ezxml_tofile(const char *file, const char *txt)
{
	int fd;
	char temp[256];
	mode_t mode = S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
	
	snprintf(temp, sizeof(temp), "%s~", file);
	umask(0);
	fd = open(temp, O_WRONLY | O_CREAT | O_TRUNC, mode);
	if (fd < 0) return -errno;
	if (write(fd, txt, strlen(txt)) < 0) return -errno;
	close(fd);
	if (rename(temp, file)) return -errno;
	return 0;
}
/* ezxml_ex.h - extensions for ezxml
 * Copyright (C) 2007 Syntech Systems Inc., Farrell J. Aultman <[email protected]>
 */

/* Copyright 2004-2006 Aaron Voisine <[email protected]>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef _EZXML_EX_H
#define _EZXML_EX_H

#ifdef __cplusplus
extern "C" {
#endif

#include "ezxml.h"

/************************************ get ************************************/

int ezxml_get_str(ezxml_t parent, const char *element, char *value, int size);
int ezxml_get_int(ezxml_t parent, const char *element, int *value);
int ezxml_get_byte(ezxml_t parent, const char *element, unsigned char *value, int base);
int ezxml_get_bool(ezxml_t parent, const char *element, /*bool*/unsigned char *value);
int ezxml_get_double(ezxml_t parent, const char *element, double *value);

/************************************ set ************************************/

/* allocates memory for the txt, unlike ezxml_set_txt */
ezxml_t ezxml_set_txt_ex(ezxml_t xml, const char *txt);

int ezxml_set_str(ezxml_t parent, const char *element, const char *value);
int ezxml_set_int(ezxml_t parent, const char *element, int value);
int ezxml_set_bool(ezxml_t parent, const char *element, unsigned char value);
int ezxml_set_double(ezxml_t parent, const char *element, double value);

/*********************************** write ***********************************/

/* atomically writes string of xml data to a file */
int ezxml_tofile(const char *file, const char *txt);

#ifdef __cplusplus
}
#endif

#endif /*_EZXML_EX_H*/
_______________________________________________
uClinux-dev mailing list
[email protected]
http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
This message was resent by [email protected]
To unsubscribe see:
http://mailman.uclinux.org/mailman/options/uclinux-dev

Reply via email to