Programming
[Xerces] XML 파일로 저장하기
정윤아빠 입니다.
2021. 3. 13. 09:30
반응형
Xerces 라이브러리를 사용한 XML 파일로 저장하기
코드 참조.
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
std::cout << "Error during initialization!" << std::endl;
return;
}
XMLCh tempStr[100];
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
auto impl = DOMImplementationRegistry::getDOMImplementation(gLS);
XMLString::transcode("company", tempStr, 99);
//(Root )노드 생성
xercesc::DOMDocument* doc = impl->createDocument(
0, // root element namespace URI.
tempStr, // root element name
0); // document type object (DTD).
DOMElement* rootElem = doc->getDocumentElement();
// node 생성와 값에 대한 값을 넣는다.
// 노드 생성
XMLString::transcode("product", tempStr, 99);
DOMElement* prodElem = doc->createElement(tempStr);
rootElem->appendChild(prodElem);
// 그 노드에 맞는 값
XMLString::transcode("Xerces-C", tempStr, 99);
DOMText* prodDataVal = doc->createTextNode(tempStr);
prodElem->appendChild(prodDataVal);
DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
// optionally you can set some features on this serializer
if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true))
theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
// optionally you can implement your DOMLSSerializerFilter (e.g. MyDOMLSSerializerFilter)
// and set it to the serializer
//DOMLSSerializer* myFilter = new myDOMLSSerializerFilter();
//theSerializer->setFilter(myFilter);
// optionally you can implement your DOMErrorHandler (e.g. MyDOMErrorHandler)
// and set it to the serializer
//DOMErrorHandler* errHandler = new myDOMErrorHandler();
//theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler);
XMLFormatTarget* myFormTarget = new LocalFileFormatTarget(file_path);
DOMLSOutput* theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
theOutput->setByteStream(myFormTarget);
try {
// do the serialization through DOMLSSerializer::write();
theSerializer->write(doc, theOutput);
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
}
catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
}
catch (...) {
cout << "Unexpected Exception \n";
}
theOutput->release();
theSerializer->release();
delete myFormTarget;
XMLPlatformUtils::Terminate();
반응형