libxml2输出文档到内在或文件中能去空白吗?

Andy84920 2007-03-30 09:51:21
如果我的文件是格式对齐的,然后我从文件恢复成一个文档,然后我想让它以压缩的方式(去掉所有对齐,第一行声明还是第一行,其它的所有节点都放在一行上),有直接的函数支持吗?好像在API文档上没查到,不知道大家用的时候是不是都是自己写trim函数的?
...全文
415 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy84920 2007-04-04
  • 打赏
  • 举报
回复
请教上面的高手。。。
Andy84920 2007-04-03
  • 打赏
  • 举报
回复
xmlTextWriterSetIndent用这个可以吗?我试了,我现在是通过一个格式对齐的XML模板文件恢复成一个xmlDoc *,然后想把这个doc dump成buff,但是要放在一行上,不需要任何换行空白(当然元素里的内容是不要改变的),如xmlDoc *trimXMLDoc(xmlDoc *)就是这样的原型。
Andy84920 2007-04-02
  • 打赏
  • 举报
回复
没有人用过吗?
DaySummer 2007-04-02
  • 打赏
  • 举报
回复
please change the "raml" words to you want to be. and xmlTextWriterSetIndent(myXMLWriter, 0); API will indent the XML file according your requirement. If you try, you will reach the goal, shut up until you try it.
DaySummer 2007-04-02
  • 打赏
  • 举报
回复
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>



static xmlTextWriterPtr myXMLWriter = NULL;
static xmlBufferPtr my_buf = NULL;
static int isDocumentStarted = 0;

#define MAX_LENGTH 128

/**
* Write a DOCTYPE declaration.
*
* @param writer The xmlTextWriterPtr
* @param name The DOCTYPE name.
* @param publicId The doctype's public identifier, or null if none was given.
* @param systemId The doctype's system identifier, or null if none was given.
*
* @return
* if there is an error
*/
static void
document_type(const xmlTextWriterPtr writer,
const char* name,
const char* publicId,
const char* systemId)
{
if(!writer)
{
return;
}
char tmp[MAX_LENGTH];
if(publicId != NULL)
{
sprintf(tmp, "<!DOCTYPE %s PUBLIC \'%s\'>\n", name, publicId);
}
else if(systemId != NULL)
{

sprintf(tmp, "<!DOCTYPE %s SYSTEM \'%s\'>\n", name, systemId);
}
else
{
fprintf(stderr, "systemId and publicId are all null. \n");
return;
}


/*tmp end is zero*/
xmlChar* xml_tmp = NULL;

/*
* encoding the tmp to the specified encode<code>RAML_ENCODING</code>,
* <code>conver_input</code> will allocate the memory for
* return value <code>xml_tmp</code> */
xml_tmp = conver_input(tmp, "UTF-8");
int rc;
rc = xmlTextWriterWriteString(writer, xml_tmp);

/*
* deallocate the xml_tmp's memory that allocated by
* <code>conver_input</code>
*/
if (xml_tmp != NULL)
{
xmlFree(xml_tmp);
}

if (rc < 0)
{
raml_debug("document_type: Error at xmlTextWriterWriteString");
return;
}

}


/*
* Starts the XML document and will store the stream into a buffer.
*/
int
start_document_string()
{
if(isDocumentStarted)
{
fprintf(stderr, "raml_start_document_string, The document has already been started.");
return GW_ERR;
}

int rc;

isDocumentStarted = GW_TRUE;

LIBXML_TEST_VERSION

/* Create a new XML buffer, to which the XML document will be
* written */
my_buf = xmlBufferCreate();
if (my_buf == NULL)
{
printf("testXmlwriterMemory: Error creating the xml buffer\n");
return GW_ERR;
}


/* Create a new XmlWriter for memory, with no compression.
* Remark: there is no compression for this kind of xmlTextWriter */
myXMLWriter = xmlNewTextWriterMemory(my_buf, 0);
if (myXMLWriter == NULL)
{
printf("testXmlwriterMemory: Error creating the xml writer\n");
return GW_ERR;
}

/* Disable the indent of xml writer*/
//xmlTextWriterSetIndent(myXMLWriter, 0);

/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration.
**/
rc = xmlTextWriterStartDocument(myXMLWriter, NULL, RAML_ENCODING, NULL);
if (rc < 0)
{
raml_debug("start_raml_document: Error at xmlTextWriterStartDocument");
return GW_ERR;
}

document_type(myXMLWriter, "raml", NULL, "raml21.dtd");


/*
* Start an element named "raml". Since thist is the first
* element, this will be the root element of the document.
**/
rc = xmlTextWriterStartElement(myXMLWriter, BAD_CAST "xxxx");
if (rc < 0)
{
raml_debug("start_raml_document: Error at xmlTextWriterStartElement");
return GW_ERR;
}

/* Add an attribute with name "version" and value "2.1" to xxxx file. */
rc = xmlTextWriterWriteAttribute(myXMLWriter, BAD_CAST "version",
BAD_CAST "2.1");
if (rc < 0)
{
raml_debug("start_raml_document: Error at xmlTextWriterWriteAttribute");
return GW_ERR;
}

/* write the namespace xmlns:xsd */
rc = xmlTextWriterWriteAttribute(myXMLWriter, BAD_CAST "xmlns:xsd",
BAD_CAST "xxxx.xsd");
if (rc < 0)
{
raml_debug
("start_raml_document: Error at xmlTextWriterWriteAttribute xmlns");
return GW_ERR;
}

return GW_OK;
}


/**
* Ends the XML document.
*/
int
raml_end_document_string(char *data)
{
if(!isDocumentStarted)
{
fprintf(stderr,
"isDocumentStarted, The document has not beed started.");
return GW_ERR;
}

int rc;

rc = xmlTextWriterEndElement(myXMLWriter);
if (rc < 0)
{
raml_debug
("raml_end_document_string: Error at xmlTextWriterEndElement");
return GW_ERR;
}

rc = xmlTextWriterEndDocument(myXMLWriter);
if (rc < 0)
{
raml_debug(
"raml_end_document_string: Error at xmlTextWriterEndDocument");
return GW_ERR;
}

isDocumentStarted = GW_FALSE;

xmlFreeTextWriter(myXMLWriter);
myXMLWriter = NULL;

int len = strlen(my_buf->content);
memset(data, 0, len + 1);
memcpy(data, my_buf->content, len);

xmlBufferFree(my_buf);
my_buf = NULL;

/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();

/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();

return GW_OK;
}

Andy84920 2007-03-31
  • 打赏
  • 举报
回复
不是说去掉节点的content中的空格,而是把N行的XML文件压缩成一行,去掉各元素之间的换行和空格,而不去掉节点值中的空格。
Andy84920 2007-03-30
  • 打赏
  • 举报
回复
有没有去掉注释后再写入文件或导出到内存?
DaySummer 2007-03-30
  • 打赏
  • 举报
回复
/* Enable the indent of xml writer*/
xmlTextWriterSetIndent(myXMLWriter, 1);
bladeIII 2007-03-30
  • 打赏
  • 举报
回复
xmlTextWriterSetIndent(writer,1); 这个函数是设置写入文件时是对齐还是不对齐的。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧