📄 sax.c
字号:
fprintf(stderr, "add reference %s to %s \n", name, ctxt->node->name);#endif xmlAddChild(ctxt->node, ret);}/** * characters: * @ctx: the user data (XML parser context) * @ch: a xmlChar string * @len: the number of xmlChar * * receiving some chars from the parser. * Question: how much at a time ??? */voidcharacters(void *ctx, const xmlChar *ch, int len){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr lastChild;#ifdef DEBUG_SAX fprintf(stderr, "SAX.characters(%.30s, %d)\n", ch, len);#endif /* * Handle the data if any. If there is no child * add it as content, otherwise if the last child is text, * concatenate it, else create a new node of type text. */ if (ctxt->node == NULL) {#ifdef DEBUG_SAX_TREE fprintf(stderr, "add chars: ctxt->node == NULL !\n");#endif return; } lastChild = xmlGetLastChild(ctxt->node);#ifdef DEBUG_SAX_TREE fprintf(stderr, "add chars to %s \n", ctxt->node->name);#endif if (lastChild == NULL) xmlNodeAddContentLen(ctxt->node, ch, len); else { if (xmlNodeIsText(lastChild)) xmlTextConcat(lastChild, ch, len); else { lastChild = xmlNewTextLen(ch, len); xmlAddChild(ctxt->node, lastChild); } }}/** * ignorableWhitespace: * @ctx: the user data (XML parser context) * @ch: a xmlChar string * @len: the number of xmlChar * * receiving some ignorable whitespaces from the parser. * Question: how much at a time ??? */voidignorableWhitespace(void *ctx, const xmlChar *ch, int len){ /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */#ifdef DEBUG_SAX fprintf(stderr, "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);#endif}/** * processingInstruction: * @ctx: the user data (XML parser context) * @target: the target name * @data: the PI data's * @len: the number of xmlChar * * A processing instruction has been parsed. */voidprocessingInstruction(void *ctx, const xmlChar *target, const xmlChar *data){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret; xmlNodePtr parent = ctxt->node;#ifdef DEBUG_SAX fprintf(stderr, "SAX.processingInstruction(%s, %s)\n", target, data);#endif ret = xmlNewPI(target, data); if (ret == NULL) return; parent = ctxt->node; if (ctxt->inSubset == 1) { xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret); return; } else if (ctxt->inSubset == 2) { xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret); return; } if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {#ifdef DEBUG_SAX_TREE fprintf(stderr, "Setting PI %s as root\n", target);#endif xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); return; } if (parent->type == XML_ELEMENT_NODE) {#ifdef DEBUG_SAX_TREE fprintf(stderr, "adding PI %s child to %s\n", target, parent->name);#endif xmlAddChild(parent, ret); } else {#ifdef DEBUG_SAX_TREE fprintf(stderr, "adding PI %s sibling to ", target); xmlDebugDumpOneNode(stderr, parent, 0);#endif xmlAddSibling(parent, ret); }}/** * globalNamespace: * @ctx: the user data (XML parser context) * @href: the namespace associated URN * @prefix: the namespace prefix * * An old global namespace has been parsed. */voidglobalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;#ifdef DEBUG_SAX fprintf(stderr, "SAX.globalNamespace(%s, %s)\n", href, prefix);#endif xmlNewGlobalNs(ctxt->myDoc, href, prefix);}/** * setNamespace: * @ctx: the user data (XML parser context) * @name: the namespace prefix * * Set the current element namespace. */voidsetNamespace(void *ctx, const xmlChar *name){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNsPtr ns; xmlNodePtr parent;#ifdef DEBUG_SAX fprintf(stderr, "SAX.setNamespace(%s)\n", name);#endif ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name); if (ns == NULL) { /* ctxt->node may not have a parent yet ! */ if (ctxt->nodeNr >= 2) { parent = ctxt->nodeTab[ctxt->nodeNr - 2]; if (parent != NULL) ns = xmlSearchNs(ctxt->myDoc, parent, name); } } xmlSetNs(ctxt->node, ns);}/** * getNamespace: * @ctx: the user data (XML parser context) * * Get the current element namespace. */xmlNsPtrgetNamespace(void *ctx){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNsPtr ret;#ifdef DEBUG_SAX fprintf(stderr, "SAX.getNamespace()\n");#endif ret = ctxt->node->ns; return(ret);}/** * checkNamespace: * @ctx: the user data (XML parser context) * @namespace: the namespace to check against * * Check that the current element namespace is the same as the * one read upon parsing. */intcheckNamespace(void *ctx, xmlChar *namespace){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr cur = ctxt->node;#ifdef DEBUG_SAX fprintf(stderr, "SAX.checkNamespace(%s)\n", namespace);#endif /* * Check that the Name in the ETag is the same as in the STag. */ if (namespace == NULL) { if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) ctxt->sax->error(ctxt, "End tags for %s don't hold the namespace %s\n", cur->name, cur->ns->prefix); ctxt->wellFormed = 0; } } else { if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) { if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) ctxt->sax->error(ctxt, "End tags %s holds a prefix %s not used by the open tag\n", cur->name, namespace); ctxt->wellFormed = 0; } else if (xmlStrcmp(namespace, cur->ns->prefix)) { if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) ctxt->sax->error(ctxt, "Start and End tags for %s don't use the same namespaces: %s and %s\n", cur->name, cur->ns->prefix, namespace); ctxt->wellFormed = 0; } else return(1); } return(0);}/** * namespaceDecl: * @ctx: the user data (XML parser context) * @href: the namespace associated URN * @prefix: the namespace prefix * * A namespace has been parsed. */voidnamespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;#ifdef DEBUG_SAX if (prefix == NULL) fprintf(stderr, "SAX.namespaceDecl(%s, NULL)\n", href); else fprintf(stderr, "SAX.namespaceDecl(%s, %s)\n", href, prefix);#endif xmlNewNs(ctxt->node, href, prefix);}/** * comment: * @ctx: the user data (XML parser context) * @value: the comment content * * A comment has been parsed. */voidcomment(void *ctx, const xmlChar *value){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret; xmlNodePtr parent = ctxt->node;#ifdef DEBUG_SAX fprintf(stderr, "SAX.comment(%s)\n", value);#endif ret = xmlNewDocComment(ctxt->myDoc, value); if (ret == NULL) return; if (ctxt->inSubset == 1) { xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret); return; } else if (ctxt->inSubset == 2) { xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret); return; } if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {#ifdef DEBUG_SAX_TREE fprintf(stderr, "Setting comment as root\n");#endif xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret); return; } if (parent->type == XML_ELEMENT_NODE) {#ifdef DEBUG_SAX_TREE fprintf(stderr, "adding comment child to %s\n", parent->name);#endif xmlAddChild(parent, ret); } else {#ifdef DEBUG_SAX_TREE fprintf(stderr, "adding comment sibling to "); xmlDebugDumpOneNode(stderr, parent, 0);#endif xmlAddSibling(parent, ret); }}/** * cdataBlock: * @ctx: the user data (XML parser context) * @value: The pcdata content * @len: the block length * * called when a pcdata block has been parsed */voidcdataBlock(void *ctx, const xmlChar *value, int len){ xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlNodePtr ret, lastChild;#ifdef DEBUG_SAX fprintf(stderr, "SAX.pcdata(%.10s, %d)\n", value, len);#endif lastChild = xmlGetLastChild(ctxt->node);#ifdef DEBUG_SAX_TREE fprintf(stderr, "add chars to %s \n", ctxt->node->name);#endif if ((lastChild != NULL) && (lastChild->type == XML_CDATA_SECTION_NODE)) { xmlTextConcat(lastChild, value, len); } else { ret = xmlNewCDataBlock(ctxt->myDoc, value, len); xmlAddChild(ctxt->node, ret); }}/* * Default handler for XML, builds the DOM tree */xmlSAXHandler xmlDefaultSAXHandler = { internalSubset, isStandalone, hasInternalSubset, hasExternalSubset, resolveEntity, getEntity, entityDecl, notationDecl, attributeDecl, elementDecl, unparsedEntityDecl, setDocumentLocator, startDocument, endDocument, startElement, endElement, reference, characters, ignorableWhitespace, processingInstruction, comment, xmlParserWarning, xmlParserError, xmlParserError, getParameterEntity, cdataBlock, externalSubset,};/** * xmlDefaultSAXHandlerInit: * * Initialize the default SAX handler */voidxmlDefaultSAXHandlerInit(void){ xmlDefaultSAXHandler.internalSubset = internalSubset; xmlDefaultSAXHandler.externalSubset = externalSubset; xmlDefaultSAXHandler.isStandalone = isStandalone; xmlDefaultSAXHandler.hasInternalSubset = hasInternalSubset; xmlDefaultSAXHandler.hasExternalSubset = hasExternalSubset; xmlDefaultSAXHandler.resolveEntity = resolveEntity; xmlDefaultSAXHandler.getEntity = getEntity; xmlDefaultSAXHandler.getParameterEntity = getParameterEntity; xmlDefaultSAXHandler.entityDecl = entityDecl; xmlDefaultSAXHandler.attributeDecl = attributeDecl; xmlDefaultSAXHandler.elementDecl = elementDecl; xmlDefaultSAXHandler.notationDecl = notationDecl; xmlDefaultSAXHandler.unparsedEntityDecl = unparsedEntityDecl; xmlDefaultSAXHandler.setDocumentLocator = setDocumentLocator; xmlDefaultSAXHandler.startDocument = startDocument; xmlDefaultSAXHandler.endDocument = endDocument; xmlDefaultSAXHandler.startElement = startElement; xmlDefaultSAXHandler.endElement = endElement; xmlDefaultSAXHandler.reference = reference; xmlDefaultSAXHandler.characters = characters; xmlDefaultSAXHandler.cdataBlock = cdataBlock; xmlDefaultSAXHandler.ignorableWhitespace = ignorableWhitespace; xmlDefaultSAXHandler.processingInstruction = processingInstruction; xmlDefaultSAXHandler.comment = comment; if (xmlGetWarningsDefaultValue == 0) xmlDefaultSAXHandler.warning = NULL; else xmlDefaultSAXHandler.warning = xmlParserWarning; xmlDefaultSAXHandler.error = xmlParserError; xmlDefaultSAXHandler.fatalError = xmlParserError;}/* * Default handler for HTML, builds the DOM tree */xmlSAXHandler htmlDefaultSAXHandler = { NULL, NULL, NULL, NULL, NULL, getEntity, NULL, NULL, NULL, NULL, NULL, setDocumentLocator, startDocument, endDocument, startElement, endElement, NULL, characters, ignorableWhitespace, NULL, comment, xmlParserWarning, xmlParserError, xmlParserError, getParameterEntity, NULL, NULL,};/** * htmlDefaultSAXHandlerInit: * * Initialize the default SAX handler */voidhtmlDefaultSAXHandlerInit(void){ htmlDefaultSAXHandler.internalSubset = NULL; htmlDefaultSAXHandler.externalSubset = NULL; htmlDefaultSAXHandler.isStandalone = NULL; htmlDefaultSAXHandler.hasInternalSubset = NULL; htmlDefaultSAXHandler.hasExternalSubset = NULL; htmlDefaultSAXHandler.resolveEntity = NULL; htmlDefaultSAXHandler.getEntity = getEntity; htmlDefaultSAXHandler.getParameterEntity = NULL; htmlDefaultSAXHandler.entityDecl = NULL; htmlDefaultSAXHandler.attributeDecl = NULL; htmlDefaultSAXHandler.elementDecl = NULL; htmlDefaultSAXHandler.notationDecl = NULL; htmlDefaultSAXHandler.unparsedEntityDecl = NULL; htmlDefaultSAXHandler.setDocumentLocator = setDocumentLocator; htmlDefaultSAXHandler.startDocument = startDocument; htmlDefaultSAXHandler.endDocument = endDocument; htmlDefaultSAXHandler.startElement = startElement; htmlDefaultSAXHandler.endElement = endElement; htmlDefaultSAXHandler.reference = NULL; htmlDefaultSAXHandler.characters = characters; htmlDefaultSAXHandler.cdataBlock = NULL; htmlDefaultSAXHandler.ignorableWhitespace = ignorableWhitespace; htmlDefaultSAXHandler.processingInstruction = NULL; htmlDefaultSAXHandler.comment = comment; htmlDefaultSAXHandler.warning = xmlParserWarning; htmlDefaultSAXHandler.error = xmlParserError; htmlDefaultSAXHandler.fatalError = xmlParserError;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -