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.
/**
* 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;
}
/*
* 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;
}