📄 xmlwriter.c
字号:
writer->ctxt->myDoc = NULL;
}
xmlFreeParserCtxt(writer->ctxt);
}
if (writer->ichar != NULL)
xmlFree(writer->ichar);
xmlFree(writer);
}
/**
* xmlTextWriterStartDocument:
* @writer: the xmlTextWriterPtr
* @version: the xml version ("1.0") or NULL for default ("1.0")
* @encoding: the encoding or NULL for default
* @standalone: "yes" or "no" or NULL for default
*
* Start a new xml document
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartDocument(xmlTextWriterPtr writer, const char *version,
const char *encoding, const char *standalone)
{
int count;
int sum;
xmlLinkPtr lk;
xmlCharEncodingHandlerPtr encoder;
if ((writer == NULL) || (writer->out == NULL)) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartDocument : invalid writer!\n");
return -1;
}
lk = xmlListFront(writer->nodes);
if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartDocument : not allowed in this context!\n");
return -1;
}
encoder = NULL;
if (encoding != NULL) {
encoder = xmlFindCharEncodingHandler(encoding);
if (encoder == NULL) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartDocument : out of memory!\n");
return -1;
}
}
writer->out->encoder = encoder;
if (encoder != NULL) {
writer->out->conv = xmlBufferCreateSize(4000);
xmlCharEncOutFunc(encoder, writer->out->conv, NULL);
} else
writer->out->conv = NULL;
sum = 0;
count = xmlOutputBufferWriteString(writer->out, "<?xml version=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
if (version != 0)
count = xmlOutputBufferWriteString(writer->out, version);
else
count = xmlOutputBufferWriteString(writer->out, "1.0");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
if (writer->out->encoder != 0) {
count = xmlOutputBufferWriteString(writer->out, " encoding=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count =
xmlOutputBufferWriteString(writer->out,
writer->out->encoder->name);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
if (standalone != 0) {
count = xmlOutputBufferWriteString(writer->out, " standalone=");
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWriteString(writer->out, standalone);
if (count < 0)
return -1;
sum += count;
count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "?>\n");
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndDocument:
* @writer: the xmlTextWriterPtr
*
* End an xml document. All open elements are closed
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndDocument(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterEndDocument : invalid writer!\n");
return -1;
}
sum = 0;
while ((lk = xmlListFront(writer->nodes)) != NULL) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
break;
switch (p->state) {
case XML_TEXTWRITER_NAME:
case XML_TEXTWRITER_ATTRIBUTE:
case XML_TEXTWRITER_TEXT:
count = xmlTextWriterEndElement(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
count = xmlTextWriterEndPI(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_CDATA:
count = xmlTextWriterEndCDATA(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_DTD:
case XML_TEXTWRITER_DTD_TEXT:
case XML_TEXTWRITER_DTD_ELEM:
case XML_TEXTWRITER_DTD_ELEM_TEXT:
case XML_TEXTWRITER_DTD_ATTL:
case XML_TEXTWRITER_DTD_ATTL_TEXT:
case XML_TEXTWRITER_DTD_ENTY:
case XML_TEXTWRITER_DTD_ENTY_TEXT:
case XML_TEXTWRITER_DTD_PENT:
count = xmlTextWriterEndDTD(writer);
if (count < 0)
return -1;
sum += count;
break;
case XML_TEXTWRITER_COMMENT:
count = xmlTextWriterEndComment(writer);
if (count < 0)
return -1;
sum += count;
break;
default:
break;
}
}
if (!writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
return sum;
}
/**
* xmlTextWriterStartComment:
* @writer: the xmlTextWriterPtr
*
* Start an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartComment(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterStartComment : invalid writer!\n");
return -1;
}
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_TEXT:
case XML_TEXTWRITER_NONE:
break;
case XML_TEXTWRITER_NAME:
count = xmlOutputBufferWriteString(writer->out, ">");
if (count < 0)
return -1;
sum += count;
if (writer->indent) {
count =
xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
p->state = XML_TEXTWRITER_TEXT;
break;
default:
return -1;
}
}
}
p = (xmlTextWriterStackEntry *)
xmlMalloc(sizeof(xmlTextWriterStackEntry));
if (p == 0) {
xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
"xmlTextWriterStartElement : out of memory!\n");
return -1;
}
p->name = 0;
p->state = XML_TEXTWRITER_COMMENT;
xmlListPushFront(writer->nodes, p);
if (writer->indent) {
count = xmlTextWriterWriteIndent(writer);
if (count < 0)
return -1;
sum += count;
}
count = xmlOutputBufferWriteString(writer->out, "<!--");
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterEndComment:
* @writer: the xmlTextWriterPtr
*
* End the current xml coment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterEndComment(xmlTextWriterPtr writer)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterEndComment : invalid writer!\n");
return -1;
}
lk = xmlListFront(writer->nodes);
if (lk == 0) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterEndComment : not allowed in this context!\n");
return -1;
}
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p == 0)
return -1;
sum = 0;
switch (p->state) {
case XML_TEXTWRITER_COMMENT:
count = xmlOutputBufferWriteString(writer->out, "-->");
if (count < 0)
return -1;
sum += count;
break;
default:
return -1;
}
if (writer->indent) {
count = xmlOutputBufferWriteString(writer->out, "\n");
if (count < 0)
return -1;
sum += count;
}
xmlListPopFront(writer->nodes);
return sum;
}
/**
* xmlTextWriterWriteFormatComment:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @...: extra parameters for the format
*
* Write an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteFormatComment(xmlTextWriterPtr writer,
const char *format, ...)
{
int rc;
va_list ap;
va_start(ap, format);
rc = xmlTextWriterWriteVFormatComment(writer, format, ap);
va_end(ap);
return rc;
}
/**
* xmlTextWriterWriteVFormatComment:
* @writer: the xmlTextWriterPtr
* @format: format string (see printf)
* @argptr: pointer to the first member of the variable argument list.
*
* Write an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteVFormatComment(xmlTextWriterPtr writer,
const char *format, va_list argptr)
{
int rc;
xmlChar *buf;
if (writer == NULL) {
xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
"xmlTextWriterWriteVFormatComment : invalid writer!\n");
return -1;
}
buf = xmlTextWriterVSprintf(format, argptr);
if (buf == 0)
return 0;
rc = xmlTextWriterWriteComment(writer, buf);
xmlFree(buf);
return rc;
}
/**
* xmlTextWriterWriteComment:
* @writer: the xmlTextWriterPtr
* @content: comment string
*
* Write an xml comment.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterWriteComment(xmlTextWriterPtr writer, const xmlChar * content)
{
int count;
int sum;
sum = 0;
count = xmlTextWriterStartComment(writer);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterWriteString(writer, content);
if (count < 0)
return -1;
sum += count;
count = xmlTextWriterEndComment(writer);
if (count < 0)
return -1;
sum += count;
return sum;
}
/**
* xmlTextWriterStartElement:
* @writer: the xmlTextWriterPtr
* @name: element name
*
* Start an xml element.
*
* Returns the bytes written (may be 0 because of buffering) or -1 in case of error
*/
int
xmlTextWriterStartElement(xmlTextWriterPtr writer, const xmlChar * name)
{
int count;
int sum;
xmlLinkPtr lk;
xmlTextWriterStackEntry *p;
if ((writer == NULL) || (name == NULL) || (*name == '\0'))
return -1;
sum = 0;
lk = xmlListFront(writer->nodes);
if (lk != 0) {
p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
if (p != 0) {
switch (p->state) {
case XML_TEXTWRITER_PI:
case XML_TEXTWRITER_PI_TEXT:
return -1;
case XML_TEXTWRITER_NONE:
break;
case XML_TEXTWRITER_ATTRIBUTE:
count = xmlTextWriterEndAttribute(writer);
if (count < 0)
return -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -