|
|
|
![]()
[edit]
1 expatÀÌ ¹¹ÁÒ? #expatÀº C·Î ÀÛ¼ºµÈ XML¹®¼¸¦ ÆÄ½ÌÇϱâ À§ÇÑ ¶óÀ̺귯¸®ÀÔ´Ï´Ù. À̰ÍÀº ¿ÀǼҽº ÇÁ·ÎÁ§Æ®ÀÎ ¸ðÁú¶ó ÇÁ·ÎÁ§Æ®, ÆÞ ¾ð¾îÀÇ XML:Parser, ±×¿Ü ´Ù¸¥ ¿ÀǼҽº XML ÆÄ¼·Î¼ ³»ÀåµÇ¾îÀÖ½À´Ï´Ù. ³ªÀÇ
º¥Ä¡¸¶Å© ¾ÆÆ¼Å¬¿¡¼ÀÇ ½Ã¿¬ µ¥ÀÌŸ¸¦ º¸¸é ÀÌ ¶óÀ̺귯¸®´Â ¸Å¿ì ºü¸£´Ù´Â °ÍÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù. ¶ÇÇÑ ¾ÈÁ¤¼º°ú °ß°íÇÔ, ¿À·ù ¼öÁ¤¿¡ ´ëÇÑ ±âÁØÀ» ³ô°Ô µÎ¾î °³¹ßµÇ¾ú½À´Ï´Ù. ÀÌ ¶óÀ̺귯¸®´Â james clark¿¡ ÀÇÇØ ¸¸µé¾îÁ³½À´Ï´Ù. (±×´Â groff(nroff¿Í ºñ½ÁÇÑ Åø), Jade(SGMLÀ» À§ÇÑ ½ºÅ¸ÀÏ½ÃÆ® ¾ð¾îÀÎ ISOÀÇ DSSSL ±¸Çö), XP(ÀÚ¹Ù XML ÆÄ¼ ÆÐŰÁö), XT(ÀÚ¹Ù XSL ¿£Áø)À» °³¹ßÇÑ °ÍÀ¸·Î À¯¸íÇÕ´Ï´Ù) James´Â ¶ÇÇÑ XML ½ºÆÑÀ» Á¦Á¤ÇÏ´Â W3ÀÇ XML Working GroupÀÇ ±â¼ú °í¹®À» ¿ªÀÓÇϰí ÀÖ½À´Ï´Ù. ÀÌµé ÆÐŰÁöÀÇ ´ëºÎºÐÀº ftp://ftp.jclark.com/pub¿¡¼ ±¸ÇÒ ¼ö ÀÖ½À´Ï´Ù.
[edit]
2 expatÀÇ °³¿ä #expatÀº ½ºÆ®¸² ±â¹Ý ÆÄ¼ÀÔ´Ï´Ù. ¿©·¯ºÐÀº ÆÄ¼¿¡ Äݹé(¶Ç´Â Çڵ鷯)¸¦ µî·ÏÇϰí, ¹®¼¸¦ Á¦´ÜÇϱ⠽ÃÀÛÇÕ´Ï´Ù. ÆÄ¼°¡ ¹®¼ÀÇ °¢ ºÎºÐÀ» ÀνÄÇÒ ¶§¸¶´Ù ±×ºÎºÐ¿¡ ¾Ë¸ÂÀº Çڵ鷯¸¦ È£ÃâÇÒ °Í ÀÔ´Ï´Ù(¸¸¾à ¿©·¯ºÐÀÌ µî·ÏÇß´Ù¸é ¸»ÀÌÁÒ) ¹®¼´Â ÆÄ¼¿¡ ¿©·¯Á¶°¢À¸·Î Á¦´ÜµÇ¹Ç·Î, ¿©·¯ºÐÀº Àüü ¹®¼¸¦ ÀбâÀü¿¡ ÆÄ½ÌÀ» ½ÃÀÛÇÒ ¼ö ÀÖ½À´Ï´Ù. ÀÌ ±â´ÉÀº ¸Þ¸ð¸®¾È¿¡ ·ÎµùÇÒ ¼ö ¾ø´Â °Å´ëÇÑ ¹®¼¸¦ ÆÄ½ÌÇÒ ¼ö ÀÖµµ·Ï ¸¸µé¾î ÁÝ´Ï´Ù.
expat¿¡ ÀÖ´Â ¸¹Àº Á¾·ùÀÇ ÇÚµé¾î¿Í ¿É¼Ç ¶§¹®¿¡ ¹Ì¸® °ÌÀ» ¸ÔÀ» ¼öµµ ÀÖÀ» °Ì´Ï´Ù. ÇÏÁö¸¸ ¿©·¯ºÐÀÌ ¿øÇÏ´Â ÀÛ¾÷ÀÇ 80%Á¤µµ´Â 4°³ÀÇ ÇÔ¼ö¸¸ ¾Ë¸é µË´Ï´Ù.
ÀÌ
zip ÈÀÏÀº ÀÌ ¾ÆÆ¼Å¬¿¡¼ »ç¿ëµÈ Makefile°ú ¼Ò½ºÄڵ带 ´ã°í ÀÖ½À´Ï´Ù.
À§ ÇÔ¼öµéÁß ´ÜÁö 3°³¸¸À» »ç¿ëÇÏ´Â ¾ÆÁÖ °£´ÜÇÑ ÇÁ·Î±×·¥À» º¸µµ·Ï ÇսôÙ(¹®ÀÚ Çڵ鷯¸¦ ¼³Á¤ÇÒ ÇÊ¿ä´Â ¾ø½À´Ï´Ù) ÇÁ·Î±×·¥
outline.cÀº ±¸¼º¿ä¼ÒÀÇ ¿ä¾àÀ» Ãâ·ÂÇϴµ¥, ºÎ¸ð ¿ä¼Ò¿Í ±¸ºÐµÇµµ·Ï ÀÚ½Ä ¿ä¼Ò´Â µé¿©¾²±âÇÏ¿© Ãâ·ÂÇÕ´Ï´Ù. start Çڵ鷯°¡ ¸ðµç ÀÛ¾÷À» ¼öÇàÇÕ´Ï´Ù. ÀÌ Çڵ鷯´Â ºÎ¸ð ±¸¼º¿ä¼ÒÀÇ °¢ ´Ü°è¸¶´Ù µé¿©¾²±â¸¦ À§ÇØ 2°³ÀÇ space ¹®ÀÚ¸¦ Ãâ·ÂÇÑ´ÙÀ½, ±¸¼º¿ä¼Ò¿Í ¼Ó¼º Á¤º¸¸¦ Ãâ·ÂÇÕ´Ï´Ù. ¸¶Áö¸·À¸·Î Depth Àü¿ª º¯¼ö¸¦ Áõ°¡½Ãŵ´Ï´Ù.
int Depth;
void
start(void *data, const char *el, const char **attr) {
int i;
for (i = 0; i < Depth; i++)
printf(" ");
printf("%s", el);
for (i = 0; attr[i]; i += 2) {
printf(" %s='%s'", attr[i], attr[i + 1]);
}
printf("\n");
Depth++;
}
end ű״ ´ÜÁö Depth¸¦ °¨¼Ò½ÃŰ´Â ÀÛ¾÷¸¸ ½ÇÇàÇÕ´Ï´Ù.
void
end(void *data, const char *el) {
Depth--;
}
ÆÄ¼¸¦ »ý¼ºÇÑ ÀÌÈÄ¿¡´Â main ÇÁ·Î±×·¥Àº ´ÜÁö ¹®¼ÀÇ ³»¿ëÀ» ÆÄ¼·Î ÆÛ³Ö´Â ÀÛ¾÷¸¸À» ½ÇÇàÇÏ¸ç ±×°Ô ÀüºÎÀÔ´Ï´Ù.
[edit]
3 expatÀ» ºôµåÇϱâ #One of the problems with using expat is that isn't packaged as a library. Instead there are four separate object files that you have to link into your application. The Makefile that builds the sample applications in this article can be used as a template.
[edit]
4 ÄÄÆÄÀÏ ¼³Á¤º¯¼öµé #There are a few compiletime macros that control how the compiled expat behaves:
If your system doesn't have memmove, but does have bcopy, then you'll want to have a macro that redfines memmove to bcopy. There's a Makefile macro that does this in the sample Makefile, XP_MM. You'll have to uncomment its definition in order to have it take effect. [edit]
5 expatÀ» ÀÌ¿ëÇÏ¿© ÀÛ¾÷Çϱâ #°³¿ä¿¡¼ ÀÌ¹Ì ¾ð±ÞÇß½À´Ï´Ù¸¸, ¹®¼´Â Çѹø¿¡ ÇÑ Á¶°¢¾¿ ³ª´©¾î ÆÄ¼¿¡ °ø±ÞµË´Ï´Ù. It is completely up to the calling application how much of the document to fit into a piece. ¿¹Á¦ ÇÁ·Î±×·¥ lineÀº À̰Ϳ¡ ´ëÇÑ ¿¹¸¦ º¸¿©ÁÖ°í ÀÖ½À´Ï´Ù. ÀÌ ¿¹Á¦´Â Çѹø¿¡ ÇÑÁÙ¾¿ ÆÄ¼¿¡ ³»¿ëÀ» Àü´ÞÇÑ´ÙÀ½, start, end, test, processing ¸í·É À̺¥Æ®¸¦ ¹®¼¿¡ ¾Ë·ÁÁÝ´Ï´Ù. ÀÌ ÇÁ·Î±×·¥¿¡ ¹®¼¸¦ ÀÔ·ÂÇØ º½À¸·Î¼ ÆÄ¼ÀÇ µ¿ÀÛ ¿ø¸®¿¡ ´ëÇÑ °³³äÀû ´À³¦À» ¾òÀ» ¼ö ÀÖÀ» °Ì´Ï´Ù.
½ºÆ®¸² ±â¹Ý ÆÄ¼¸¦ »ç¿ëÇÏ¿© ¹®¼ÀÇ °¢ °èÃþÀ» °Ë»öÇÏ´Â °ÍÀº ÇöÀç ¹®¸ÆÀ» ÃßÀûÇϱâÀ§ÇÑ ½ºÅÃÀ» ÇÊ¿ä·Î ÇÕ´Ï´Ù. ¿¹¸¦ µé¾î "ÀÌ ¹®ÀÚ¿ÀÌ ¼ÓÇØÀÖ´Â ±¸¼º¿ä¼Ò űװ¡ ¹¹Áö?"¸¦ ±¸ÇöÇÏ·Á¸é ½ºÅÃÀÌ ÇÊ¿äÇѵ¥, ÀÌ´Â ÆÄ¼´Â ÇöÀç ¿ä¼ÒÀÇ ÀڽĿä¼ÒÀÎ ´Ù¸¥ ÅÂ±× ¿ä¼Ò·Î À̵¿Çß°í ÀÌ ¹®ÀÚ¿Àº ÀÌ¹Ì Áö³ªÃĹö·È±â ¶§¹®ÀÔ´Ï´Ù.
½ºÅÃÀ» »ç¿ëÇÏ¿© ¿©·¯ºÐÀÌ ÇϰíÀÚ ÇÏ´Â °ÍÀº ÇöÀç ¿·ÁÀÖ´Â ¿ä¼Ò ÅÂ±×¿Í ±× ¼Ó¼ºÀ» ¾Ë¾Æ³»´Â °Í ÀÔ´Ï´Ù. ¿©·¯ºÐÀº ÀÌ Á¤º¸¸¦ start Çڵ鷯¿¡¼ pushÇϰí end Çڵ鷯¿¡¼ popÇÏ´Â ¹æ½ÄÀ¸·Î ó¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.
¸î¸î °æ¿ì¿¡¼´Â ½ºÅÃÀÇ ±íÀ̰¡ ¾ó¸¶ÀÎÁö ¾Ë¾Æ³»´Â °Í¸¸À¸·Îµµ ÃæºÐÇÒ ¼ö ÀÖ½À´Ï´Ù. outline ¿¹Á¦¿¡¼´Â ÀÌ¿Í °°Àº °æ¿ì¸¦ ±¸ÇöÇÑ °ÍÀÔ´Ï´Ù. ÀÌ·¯ÇÑ ¶Ç´Ù¸¥ 󸮴 ¿ÏÀüÇÑ ±¸¼º ű׽ÖÀ¸·Î ÆÇÁ¤µÇ¸é 󸮸¦ °Ç³Ê¶Ù´Â °ÍÀÔ´Ï´Ù. °Ç³Ê¶Ù°íÀÚÇÏ´Â ±¸¼º Å×±×ÀÇ ½ÃÀÛ Å±׸¦ º¸¾Ò´Ù¸é skip ¿É¼Ç º¯¼ö¿¡ °ªÀ» ¼³Á¤Çϰí, ±× ±¸¼º¿ä¼Ò°¡ ½ÃÀÛÇßÀ» ¶§ÀÇ ±íÀ̸¦ ÀúÀåÇÕ´Ï´Ù. end ÅÂ±× Çڵ鷯°¡ ÀúÀåµÈ ±íÀÌÀ϶§ È£ÃâµÇ¸é, ¿©±â±îÁö°¡ °Ç³Ê¶Û ±¸¼º¿ä¼ÒÀÇ ³¡À̹ǷΠ¿É¼Ç º¯¼ö°ªÀ» ÇØÁ¦»óÅ·Π¼³Á¤ÇÕ´Ï´Ù. ¸¸¾à root ±¸¼º¿ä¼Ò°¡ 1ºÎÅÍ ½ÃÀÛÇÑ´Ù´Â °ü¿ë¹ýÀ» µû¸¥´Ù¸é, °Ç³Ê¶Ù±â(skip) ¿É¼Ç°ú °Ç³Ê¶Ù±â ±íÀÌ º¯¼ö¿¡ ´ëÇØ °°Àº º¯¼ö¸¦ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
void
init_info(Parseinfo *info) {
info->skip = 0;
info->depth = 1;
/* Other initializations here */
} /* End of init_info */
void
rawstart(void *data, const char *el, const char **attr) {
Parseinfo *inf = (Parseinfo *) data;
if (! inf->skip) {
if (should_skip(inf, el, attr)) {
inf->skip = inf->depth;
}
else
start(inf, el, attr); /* This does rest of start handling */
}
inf->depth++;
} /* End of rawstart */
void
rawend(void *data, const char *el) {
Parseinfo *inf = (Parseinfo *) data;
inf->depth--;
if (! inf->skip)
end(inf, el); /* This does rest of end handling */
if (inf->skip == inf->depth)
inf->skip = 0;
} /* End rawend */
Notice in the above example the difference in how depth is manipulated in the start and end handlers. The end tag handler should be the mirror image of the start tag handler. This is necessary to properly model containment. Since, in the start tag handler, we incremented depth after the main body of start tag code, then in the end handler, we need to manipulate it before the main body. If we'd decided to increment it first thing in the start handler, then we'd have had to decrement it last thing in the end handler.
[edit]
6 Çڵ鷯°£¿¡ Åë½ÅÇϱâ #Àü¿ª º¯¼öµéÀ» »ç¿ëÇÏÁö ¾Ê°í ¼·Î ´Ù¸¥ Çڵ鷯 °£¿¡ Á¤º¸¸¦ Àü´ÞÇÒ ¼ö ÀÖÀ¸·Á¸é °øÀ¯ÇÒ º¯¼öµéÀ» ´ãÀ» µ¥ÀÌŸ ±¸Á¶Ã¼¸¦ Á¤ÀÇÇØ¾ß¸¸ ÇÕ´Ï´Ù. ¿©·¯ºÐÀº Çڵ鷯µé¿¡°Ô ÀÌ ±¸Á¶Ã¼ÀÇ Æ÷ÀÎÅ͸¦ Àü´ÞÇϱâ À§ÇØ (XML_SetUserData ÇÔ¼ö¸¦ »ç¿ëÇÏ¿©) expat¿¡ ÀÌ Æ÷ÀÎÅ͸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù. ÀÌ °ªÀº ´ëºÎºÐÀÇ Çڵ鷯µéÀÇ Ã¹¹øÂ° ¸Å°³º¯¼ö·Î ÁöÁ¤µÇ¾îÀÖ½À´Ï´Ù.
[edit]
7 ³×ÀÓ½ºÆäÀ̽º ó¸® #ÆÄ¼¸¦ »ý¼ºÇÒ ¶§ XML_ParserCreateNSÇÔ¼ö¸¦ »ç¿ëÇϸé expatÀº À̸§°ø°£Ã³¸®¸¦ ½ÇÇàÇÕ´Ï´Ù. À̸§°ø°£ ó¸®ÇÏ¿¡¼´Â expatÀº xmlns¿Í xmlns: ¼Ó¼ºÀ» ó¸®Çϴµ¥, À̵éÀº ÁöÁ¤µÈ ±¸¼º¿ä¼ÒÀÇ À¯È¿¹üÀ§¸¦ À§ÇÑ À̸§°ø°£À» Á¤ÀÇÇÕ´Ï´Ù. ÀÌ´Â ½ÃÀÛ Çڵ鷯¿¡¼´Â ÀÌµé ¼Ó¼ºÀ» ¾ËÁö ¸øÇÑ´Ù´Â °ÍÀ» ÀǹÌÇÕ´Ï´Ù. ÇÏÁö¸¸ ¾îÇø®ÄÉÀ̼ÇÀº XML_SetNamespaceDeclHandler·Î »ç¿ëÇÏ¿© À̸§°ø°£ ó¸® Çڵ鷯¸¦ ¼³Á¤ÇÔÀ¸·Î¼ À̵é Á¤ÀÇ¿¡ ´ëÇÏ¿© ó¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.
ÁÖ¾îÁø À̸§°ø°£¿¡ ¼ÓÇÑ ±¸¼º¿ä¼Ò ŸÀÔ°ú ¼Ó¼º¸íÀº È®Àå ¾ç½ÄÀ» °¡Áø ¾Ë¸ÂÀº Çڵ鷯·Î Àü´ÞµË´Ï´Ù. ÀÌ È®Àå¾ç½ÄÀº À̸§°ø°£ URI, ±¸ºÐÀÚ ¹®ÀÚ(XML_ParserCreateNSÀÇ µÎ¹øÂ° ¸Å°³º¯¼ö)¿Í Áö¿ª¸í(¿¹:ÄÝ·Ð ¹®ÀÚ ÀÌÈÄ ºÎºÐ)ÀÔ´Ï´Ù.
You can set handlers for the start of a namespace declaration and for the end of a scope of a declaration with the XML_SetNamespaceDeclHandler function. The StartNamespaceDeclHandler is called prior to the start tag handler and the EndNamespaceDeclHandler is called before the corresponding end tag that ends the namespace's scope. The namespace start handler gets passed the prefix and URI for the namespace. For a default namespace declaration (xmlns='...'), the prefix will be null. The URI will be null for the case where the default namespace is being unset. The namespace end handler just gets the prefix for the closing scope.
These handlers are called for each declaration. So if, for instance, a start tag had three namespace declarations, then the StartNamespaceDeclHandler would be called three times before the start tag handler is called, once for each declaration.
The namespace.c example demonstrates the use of these features. Like outline.c, it produces an outline, but in addition it annotates when a namespace scope starts and when it ends. This example also demonstrates use of application user data.
[edit]
8 ¹®ÀÚ ÀÎÄÚµù #While XML is based on Unicode, and every XML processor is required to recognized UTF-8 and UTF-16 (1 and 2 byte encodings of Unicode), other encodings may be declared in XML documents or entities. For the main document, an XML declaration may contain an encoding declaration:
<?xml version="1.0" encoding="ISO-8859-2"?> External parsed entities may begin with a text declaration, which looks like an XML declaration with just an encoding declaration:
<?xml encoding="Big5"?> With expat, you may also specify an encoding at the time of creating a parser. This is useful when the encoding information may come from a source outside the document itself (like a higher level protocol.)
expat¿¡´Â 4°¡Áö ³»Àå ÀÎÄÚµù ¼³Á¤À» Áö¿øÇÕ´Ï´Ù:
Expat places restrictions on character encodings that it can support by filling in the XML_Encoding structure. include file:
One pitfall that novice expat users are likely to fall into is that although expat may accept input in various encodings, the strings that it passes to the handlers are always encoded in UTF-8. Your application is responsible for any translation of these strings into other encodings.
[edit]
9 ¿Ü¹« °³Ã¼ ÂüÁ¶¸¦ ´Ù·ç±â #Expat does not read or parse external entities directly. Note that any external DTD is a special case of an external entity. If you've set no ExternalEntityRefHandler, then external entity references are silently ignored. Otherwise, it calls your handler with the information needed to read and parse the external entity.
Your handler isn't actually responsible for parsing the entity, but it is responsible for creating a subsidiary parser with XML_ExternalEntityParserCreate that will do the job. This returns an instance of XML_Parser that has handlers and other data structures initialized from the parent parser. You may then use XML_Parse or XML_ParseBuffer calls against this parser. Since external entities my refer to other external entities, your handler should be prepared to be called recursively.
[edit]
10 DTD ÆÄ½Ì #In order to parse parameter entities, the macro XML_DTD, must be defined when expat is compiled. In addition, after creating the parser and before starting the parse, you must call XML_SetParamEntityParsing with one of the following arguments:
In order to read an external subset, you also have to set an external entity reference handler as described above. |
|
|||||||||||||||||||||||||||