SCEW 라이브러리의 element를 패스로 표현하기
글쓴이: h2h1995 / 작성시간: 월, 2010/11/15 - 2:24오후
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:
| 첨부 | 파일 크기 |
|---|---|
| 1.29 KB |
Forums:


댓글 달기