
xml_set_element_handler 함수는 XML 문서의 시작 태그와 끝 태그를 감지하여 처리하는 함수입니다. 이 함수는 두 개의 콜백 함수를 받아들입니다. 하나는 시작 태그를 감지할 때 호출되는 함수이고, 다른 하나는 끝 태그를 감지할 때 호출되는 함수입니다.
콜백 함수의 파라미터는 다음과 같습니다.
- start_element_handler: 시작 태그를 감지할 때 호출되는 함수
- 파라미터: xmlParserCtxtPtr, const xmlChar *const *atts
- xmlParserCtxtPtr: XML 파서 컨텍스트 포인터
- const xmlChar *const *atts: 태그의 속성 목록
- end_element_handler: 끝 태그를 감지할 때 호출되는 함수
- 파라미터: xmlParserCtxtPtr, const xmlChar *const *atts
- xmlParserCtxtPtr: XML 파서 컨텍스트 포인터
- const xmlChar *const *atts: 태그의 속성 목록
위의 예시에서, 시작 태그를 감지할 때 호출되는 콜백 함수는 다음과 같이 정의할 수 있습니다.
#hostingforum.kr
c
void start_element_handler(void *userData, const xmlChar *const *atts) {
// 시작 태그를 감지할 때 호출되는 함수
// userData: 사용자 데이터 포인터
// atts: 태그의 속성 목록
printf("시작 태그: %sn", atts[0]);
}
끝 태그를 감지할 때 호출되는 콜백 함수는 다음과 같이 정의할 수 있습니다.
#hostingforum.kr
c
void end_element_handler(void *userData, const xmlChar *const *atts) {
// 끝 태그를 감지할 때 호출되는 함수
// userData: 사용자 데이터 포인터
// atts: 태그의 속성 목록
printf("끝 태그: %sn", atts[0]);
}
xml_set_element_handler 함수를 사용하여 XML 문서의 시작 태그와 끝 태그를 처리할 수 있습니다.
#hostingforum.kr
c
#include
#include
int main() {
// XML 문서를 파싱할 때 사용할 파서 컨텍스트를 생성합니다.
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
// 콜백 함수를 등록합니다.
xmlSetElementHandler(ctxt, start_element_handler, end_element_handler);
// XML 문서를 파싱합니다.
xmlDocPtr doc = xmlCtxtReadFile(ctxt, "example.xml", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
// 파서 컨텍스트를 해제합니다.
xmlFreeParserCtxt(ctxt);
// XML 문서를 해제합니다.
xmlFreeDoc(doc);
return 0;
}
위의 예시에서, `start_element_handler` 함수는 시작 태그를 감지할 때 호출되며, `end_element_handler` 함수는 끝 태그를 감지할 때 호출됩니다.
2025-07-11 19:01