|
#include "XmlWriterDocument.h"
#include "XmlNode.h"
#include "XmlAttribute.h"
#if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
#define MY_ENCODING "ISO-8859-1"
#endif
CXmlWriterDocument::CXmlWriterDocument(void)
{
}
CXmlWriterDocument::~CXmlWriterDocument(void)
{
}
//Operations
int CXmlWriterDocument::WriteNode(CXmlNode* pNode, xmlTextWriterPtr writer)
{
std::string text = pNode->GetNodeName();
int rc = xmlTextWriterStartElement(writer, BAD_CAST (xmlChar*)text.c_str());
if ( rc < 0)
return rc;
for( vector<int>::size_type i = 0; i < pNode->m_ListAtts.size(); ++i )
{
CXmlAttribute* pAttr = pNode->m_ListAtts[i];
std::string name = pAttr->GetName();
std::string value = pAttr->GetValue();
/* Add an attribute with name "version" and value "1.0" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST (xmlChar*)name.c_str(),
BAD_CAST (xmlChar*)value.c_str() );
if (rc < 0)
{
return rc;
break;
}
}
for( vector<int>::size_type i = 0; i < pNode->m_ListChilds.size(); ++i )
{
rc = this->WriteNode( pNode->m_ListChilds[i], writer );
if ( rc < 0 )
{
return rc;
break;
}
}
rc = xmlTextWriterEndElement(writer);
if ( rc < 0 )
return rc;
return 0;
}
void CXmlWriterDocument::WriteFile(std::string strFileName, CXmlNode* pRoot)
{
#if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
int rc;
xmlTextWriterPtr writer;
xmlChar *tmp;
xmlDocPtr doc;
if ( pRoot == NULL )
return;
/* Create a new XmlWriter for DOM, with no compression. */
writer = xmlNewTextWriterDoc(&doc, 0);
/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration. */
rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartDocument\n");
return;
}
this->WriteNode(pRoot, writer);
xmlFreeTextWriter(writer);
xmlSaveFileEnc(strFileName.c_str(), doc, MY_ENCODING);
xmlFreeDoc(doc);
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
#endif
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I live in Matão, a small city in Brazil. I studied as Programmer in a College for Software Development in Database.
After finishing the College I have been working with java, c# and Computer Graphics with searches for OpenGL.