`

Xerces-C++学习之——创建XML文档 (转)

 
阅读更多
代码如下:
/*
* ===========================================================================
*
* Filename: CreateXML.cpp
*
* Description: This is an example of the use of Xerces-C++ operation XML.
*
* Version: 1.0
* Created: 02/28/2010 11:48:26 AM
* Revision: none
* Compiler: gcc
*
* Author: huabo (caodaijun), caodaijun@feinno.com
* Company: feinno
*
* ===========================================================================
*/
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>

#include <string>

#if defined(XERCES_NEW_IOSTREAMS)
#include <iostream>
#else
#include <iostream.h>
#endif

XERCES_CPP_NAMESPACE_USE
class XStr
{
public:
XStr(const char* const toTranscode)
{
fUnicodeForm = XMLString::transcode(toTranscode);
}
~XStr()
{
XMLString::release(&fUnicodeForm);
}
const XMLCh* unicodeForm() const
{
return fUnicodeForm;
}
private:
XMLCh* fUnicodeForm;
};

#define X(str) XStr(str).unicodeForm()

std::string basic_string =
"<basic>"
"<corp_name>abcd</corp_name>"
"<corp_code></corp_code>"
"<c0></c0>"
"<short_name></short_name>"
"<calling_code></calling_code>"
"<legal_representative></legal_representative>"
"<address></address>"
"<zip></zip>"
"<telephone></telephone>"
"<fax></fax>"
"<contact></contact>"
"<proportion_code></proportion_code>"
"<logo_crc></logo_crc>"
"<employee_portrait_crc></employee_portrait_crc>"
"<group_portrait_crc></group_portrait_crc>"
"<createtime></createtime>"
"<opentime></opentime>"
"<province_code></province_code>"
"<vgop_code></vgop_code>"
"<org_version></org_version>"
"<source_code></source_code>"
"<client_dept_version></client_dept_version>"
"<version_id></version_id>"
"<contact_mp></contact_mp>"
"</basic>";

std::string rules_string =
"<rules>"
"<order_flag></order_flag>"
"<demo_flag></demo_flag>"
"<useroperate_limit></useroperate_limit>"
"<emp_portrait_flag></emp_portrait_flag>"
"<iplmt></iplmt>"
"<dept_auth_switch></dept_auth_switch>"
"<sms_block></sms_block>"
"<sms_begin></sms_begin>"
"<sms_end></sms_end>"
"<edit_status></edit_status>"
"<expire_time></expire_time>"
"</rules>";

std::string accounts_string =
"<accounts>"
"<scale_code></scale_code>"
"<customer_manager></customer_manager>"
"<manager_phone></manager_phone>"
"<deposit_bank></deposit_bank>"
"<bank_accounts></bank_accounts>"
"<password_paper_id></password_paper_id>"
"<group_code></group_code>"
"<sms_code></sms_code>"
"<meeting_code></meeting_code>"
"<fee_code></fee_code>"
"<cycle_code></cycle_code>"
"<sharedisk_code></sharedisk_code>"
"<sp_status></sp_status>"
"<updatetime></updatetime>"
"</accounts>";

int main(int argC, char*argV[])
{
// Initialize the XML4C2 system.
try
{
XMLPlatformUtils::Initialize();
}
catch(const XMLException& toCatch)
{
char *pMsg = XMLString::transcode(toCatch.getMessage());
XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
<< " Exception message:"
<< pMsg;
XMLString::release(&pMsg);
return 1;
}

DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("LS"));
DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
MemBufFormatTarget* target = new MemBufFormatTarget();
theOutputDesc->setByteStream(target);

if (impl != NULL)
{
try
{
DOMDocument* doc = impl->createDocument(
0, // root element namespace URI.
X("corporation"), // root element name
0); // document type object (DTD).
doc->setXmlStandalone(true);

DOMElement* rootElem = doc->getDocumentElement();
rootElem->setAttribute(X("xmlns"), X("com:cmcc:corporation"));
rootElem->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance"));
rootElem->setAttribute(X("xsi:schemaLocation"), X("com:cmcc:corporation corporation.xsd"));

//add node
DOMElement* prodElem = doc->createElement(X("eid"));
rootElem->appendChild(prodElem);

DOMText* prodDataVal = doc->createTextNode(X("100000"));
prodElem->appendChild(prodDataVal);

XercesDOMParser* parser = new XercesDOMParser();

//add basic child
MemBufInputSource* basic_mem = new MemBufInputSource(
(const XMLByte* )basic_string.c_str(),
strlen(basic_string.c_str()),
"basic",
false);
parser->parse( *basic_mem );
DOMDocument* xmlDoc = parser->getDocument();
DOMElement* basic_root = xmlDoc->getDocumentElement();
DOMNode* newnode = doc->importNode((DOMNode* )basic_root, true);
rootElem->appendChild(newnode);
delete basic_mem;

//add the rules child
MemBufInputSource* rules_mem = new MemBufInputSource(
(const XMLByte* )rules_string.c_str(),
rules_string.length(),
"rules",
false);
parser->parse( *rules_mem );
xmlDoc = parser->getDocument();
DOMElement* rules_root = xmlDoc->getDocumentElement();
newnode = doc->importNode((DOMNode* )rules_root, true);
rootElem->appendChild(newnode);
delete rules_mem;

//add the accounts child
MemBufInputSource* accounts_mem = new MemBufInputSource(
(const XMLByte* )accounts_string.c_str(),
accounts_string.length(),
"accounts",
false);
parser->parse( *accounts_mem );
xmlDoc = parser->getDocument();
DOMElement* accounts_root = xmlDoc->getDocumentElement();
newnode = doc->importNode((DOMNode* )accounts_root, true);
rootElem->appendChild(newnode);
delete accounts_mem;

theSerializer->write(doc, theOutputDesc);
std::cout<< target->getRawBuffer()<< std::endl;

delete target;
delete parser;
theOutputDesc->release();
theSerializer->release();
doc->release();
}
catch(const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
}
catch (const DOMException& e)
{
XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
}
catch (...)
{
XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
}
}
else
{
XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
}

XMLPlatformUtils::Terminate();
return 0;
}
<!-- <div class="Blog_con3_1">管理员在2009年8月13日编辑了该文章文章。</div> -->
分享到:
评论

相关推荐

    xerces-c++-3.1.3

    Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将...

    Xerces-J-tools.2.11.0-xml-schema-1.1-beta.zip下载

    Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将...

    xerces-c-3.0.0-x86_64-windows-vc-9.0.rar

    xerces-c-3.0.0 编译好的库,可以直接... 这两个项目是 Apache XML 组的核心项目(如果看到的是“Xerces-C”而不是“Xerces-C++”,也是同一个东西,因为这个项目一开始就是用 C(译者注:原文为C++)语言编写的)。

    Xerces-c++指南

    Xerces-c++指南

    xerces-c-3.2.3.zip

    xerces-c-3.2.3的64位库,由VS 2015编译而成。Xerces是由Apache组织所推动的一项XML文档解析开源项目,它目前有多种语言版本包括JAVA、C++、PERL、COM等。

    Xerces-C++

    Xerces-C++ XML解析Xerces-C++ Xerces-C++ DOM编程指南

    xerces src_2_8_0

    C++开源的xml解析器,当前最新的版本xerces-c-src_2_8_0。Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的...Xerces-C++是一个非常健壮的 XML 解析器,其提供的两种解析XML文档的方法,DOM和SAX。

    xerces-c-3.2.3.tar.gz

    Apache Xerces-C validating XML parser Releases文件,下载下来可以直接使用

    d2school-Xerces-C++编译库part01

    Xerces-C++ (通常也称为:Xerces-C)是一套健壮、强大(同时也很庞大)的 XML 解析库,它提供了验证,以及 SAX 和 DOM API。来源:http://www.d2school.com/

    Xerces

    Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),...

    d2school-Xerces-C++编译库part02

    Xerces-C++ (通常也称为:Xerces-C)是一套健壮、强大(同时也很庞大)的 XML 解析库,它提供了验证,以及 SAX 和 DOM API。来源:http://www.d2school.com/

    MacOS:Xerces

    Xerces-C++ is a validating XML parser written in a portable subset of C++. Xerces-C++ makes it easy to give your application the ability to read and write XML data. A shared library is provided for ...

    demo_of_xerces-c++_MemoryManagement

    抽取xerces-c++内存管理部分代码+可作为学习demo使用。

    xml解析相关:xerces-c-3.2.3.tar、tinyxml_2_6_2、tinyxml2-master

    xml解析相关:xerces-c-3.2.3.tar、tinyxml_2_6_2、tinyxml2-master。都是源码

    xerces-c-3.2.2 (1).zip

    Xerces是由Apache组织所推动的一项XML文档解析开源项目,Xerces是一个与可扩展标记语言(XML)兼容的语法分析器。Xerces分析器可处理Java和C++,它采用互联网联盟XML、文件对象模型以及用于XML的简单API标准。所有的...

    xerces-c-3.0.0.zip

    Xerces-C++ is a validating XML parser written in a portable subset of C++. Xerces-C++ makes it easy to give your application the ability to read and write XML data. A shared library is provided for ...

    xerces-c-src_2_8_0安装&开发文档.doc

    xerces-c-src_2_8_0安装&开发文档.doc

    xerces-c-3.1.1-x86-windows-vc-9.0.zip

    XML解析xerces VS2008使用

    Xerces-J-bin.2.9.1.zip

    用来处理XML文档的分析器

Global site tag (gtag.js) - Google Analytics