SCEW 라이브러리의 element를 패스로 표현하기

h2h1995의 이미지

SCEW 라이브러리는 C에서 사용가능한 expat 래퍼 XML 라이브러리이다.

SCEW 라이브러리를 이용하면 expat 보다 더 상위레벨에서 좀더 쉽게 XML 데이타를 취급할 수 있다.

하지만 좀더 쉽게 접근하기 위한 패스를 제공하기 위해 아래와 같이 함수를 작성해 봤다.

아래와 같은 xml 이 있다고 할때.. c 노드를 패스로 표현하면 /a/b/c 로 표현된다.

<a>
    <b>
        <c>
        </c>
    </b>
</a>

나는 여기서 "/a/b/c" 표현으로 c 노드의 element를 얻고 싶다.

아래는 scew 를 이용하여 get_node_path 를 구현한 함수이며, 이를 이용한 데모는 첨부 파일에서 받길 바란다.

아래의 코드는 mingw, linux 에서 테스트 되었다.

char *get_attribute(scew_element *node, char *name)
{
	if (node != NULL)
	{
		scew_list *list = scew_element_attributes (node);
		while (list != NULL)
		{
			scew_attribute *attribute = scew_list_data (list);
			if ( !strcmp(scew_attribute_name (attribute), name) )
				return (char *)scew_attribute_value (attribute);
 
			list = scew_list_next (list);
		}
	}
}
 
char *get_node_path(scew_element *node)
{
	static char result_path[9072];
 
	scew_element *parent = node;
 
	sprintf(result_path, "");
 
	while(parent)
	{
		char *name = (char *)scew_element_name(parent);
 
		char tmp[9072];
		sprintf(tmp, "%s", result_path);
 
		sprintf(result_path, "%s", "/");
		sprintf(result_path, "%s%s", result_path, name);
		sprintf(result_path, "%s%s", result_path, tmp);
 
		parent = scew_element_parent(parent);
	}
 
	return result_path;
}
 
int get_node_depth(scew_element *node)
{
	int depth=0;
	scew_element *parent = node;
 
	while(parent)
	{
		++depth;
		parent = scew_element_parent(parent);
	}
 
	return depth;
}
 
scew_list* find_nodes (scew_element *node, char *find_path)
{
	static scew_list *result_list=NULL;
 
	char *path = get_node_path(node);
	if ( !strcmp(find_path, path) || !(strcmp(find_path, "*")) )
	{
		if(result_list==NULL)
			result_list = scew_list_create(node);
		else
			scew_list_append(result_list, node);
	}
 
	scew_list *children_list = scew_element_children (node);
	while (children_list != NULL)
	{
		scew_element *child = scew_list_data (children_list);
		result_list = find_nodes (child, find_path);
		children_list = scew_list_next (children_list);
	}
 
	return result_list;
}
 
...
char *path = "/ctml/speciesData/species/thermo/NASA/floatArray";
node_list = find_nodes (scew_tree_root (xml), path);
 
// 리스트를 검색해서 contents 를 뽑아낸다.
while (node_list != NULL)
{
        // 다음 노드를 얻는다.
        node_list = scew_list_next (node_list);
}
....
File attachments: 
첨부파일 크기
Package icon xml_read.zip1.29 KB
Forums: 

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.