Xerces-C 在根名称中放置冒号时失败
Xerces-C failing while putting colon in root name
我正在尝试通过 C++ 中的 xerces 创建 soap WSDL。我只想创建 XML 并将其放在一个文件中。
在创建 xml 时,我使用以下代码片段创建 xml 文档
pDomDocument = p_DOMImplementation->createDocument("http://schemas.xmlsoap.org/soap/envelope/",
convertStringToXmlCh("soapenv:Envelope"),
0);
但是这一行在 运行ning 时抛出异常。异常是:"Caught dom exception with message :invalid or illegal XML character".
此外,我的 xml 除了默认命名空间外还有三个命名空间。我如何通过 xerces 添加它。
但是我在 createDocument 函数中从根名称中删除冒号 (:) 的时间 运行。你能告诉我如何通过 xerces 将命名空间名称放入 xml 文档中吗?
我尝试在 google 上搜索了很多,但都没有成功。
编辑 1:
DOMImplementation* p_DOMImplementation = NULL;
p_DOMImplementation = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("core"));
DOMDocument* pDomDocument=NULL;
try
{
pDomDocument = p_DOMImplementation->createDocument(convertStringToXmlCh(uri), //Root element namespace URI
convertStringToXmlCh(rootNode), // Root element name
0); //Document type (DTD)
}
catch(DOMException& e)
{
BL_LOG_MSG(BL_FATAL,"Caught dom exception with message :<1>",XMLString::transcode(e.getMessage()))
return NULL;
}
编辑 2:
下面是我的确切代码仍然出现异常:
int main()
{
DOMImplementation* p_DOMImplementation = NULL;
XMLPlatformUtils::Initialize();
p_DOMImplementation = DOMImplementationRegistry::getDOMImplementation(convertStringToXmlCh("Core"));
DOMDocument* pDomDocument=NULL;
try
{
pDomDocument = p_DOMImplementation->createDocument(convertStringToXmlCh("http://schemas.xmlsoap.org/soap/envelope/"), //Root element namespace URI
convertStringToXmlCh("soapenv:envelope"), // Root element name
NULL); //Document type (DTD)
}
catch(DOMException& e)
{
cout <<"Caught xception:"<<e.getMessage();
return NULL;
}
catch(...)
{
cout <<"Caught xception:";
return NULL;
}
if(pDomDocument == NULL)
{
return NULL;
cout<<"NULL returned";
}
DOMElement* pRootElement = pDomDocument->getDocumentElement();
addAttributeToDomElementNS(pRootElement,"xmlns:typ","http://abcd.fg/V1/types");
char* path = "/users/gen/app/mayank.xml";
SerializeXmlWriter(pDomDocument,path);
return 0;
}
低于异常:"Caught xception:invalid or illegal XML character"
这是一个基于 Xerces 示例 CreateDOMDocument
的示例,仅在命名空间中创建元素。该示例在 Window 10 上对我来说工作正常,针对 xerces-c-3.1.1-x86-windows-vc-10.0:
的二进制版本编译
#include "stdafx.h"
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#if defined(XERCES_NEW_IOSTREAMS)
#include <iostream>
#else
#include <iostream.h>
#endif
XERCES_CPP_NAMESPACE_USE
// ---------------------------------------------------------------------------
// This is a simple class that lets us do easy (though not terribly efficient)
// trancoding of char* data to XMLCh data.
// ---------------------------------------------------------------------------
class XStr
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
XStr(const char* const toTranscode)
{
// Call the private transcoding method
fUnicodeForm = XMLString::transcode(toTranscode);
}
~XStr()
{
XMLString::release(&fUnicodeForm);
}
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
const XMLCh* unicodeForm() const
{
return fUnicodeForm;
}
private:
// -----------------------------------------------------------------------
// Private data members
//
// fUnicodeForm
// This is the Unicode XMLCh format of the string.
// -----------------------------------------------------------------------
XMLCh* fUnicodeForm;
};
#define X(str) XStr(str).unicodeForm()
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int main(int argC, char*[])
{
// 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;
}
// Watch for special case help request
int errorCode = 0;
if (argC > 1)
{
XERCES_STD_QUALIFIER cout << "\nUsage:\n"
" CreateDOMDocument\n\n"
"This program creates a new DOM document from scratch in memory.\n"
"It then prints the count of elements in the tree.\n"
<< XERCES_STD_QUALIFIER endl;
errorCode = 1;
}
if (errorCode) {
XMLPlatformUtils::Terminate();
return errorCode;
}
{
// Nest entire test in an inner block.
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (impl != NULL)
{
try
{
DOMDocument* doc = impl->createDocument(
X("http://www.w3.org/2000/svg"), // root element namespace URI.
X("svg:svg"), // root element name
0); // document type object (DTD).
doc->getDocumentElement()->appendChild(doc->createElementNS(X("http://www.w3.org/2000/svg"), X("svg:circle")));
//
// Now count the number of elements in the above DOM tree.
//
const XMLSize_t elementCount = doc->getElementsByTagName(X("*"))->getLength();
XERCES_STD_QUALIFIER cout << "The tree just created contains: " << elementCount
<< " elements." << XERCES_STD_QUALIFIER endl;
DOMImplementationLS* lsImpl = (DOMImplementationLS*)impl;
DOMLSSerializer* ser = lsImpl->createLSSerializer();
XERCES_STD_QUALIFIER wcout << ser->writeToString(doc);
doc->release();
ser->release();
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
errorCode = 5;
}
catch (const DOMException& e)
{
XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
errorCode = 2;
}
catch (...)
{
XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
errorCode = 3;
}
} // (inpl != NULL)
else
{
XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
errorCode = 4;
}
}
XMLPlatformUtils::Terminate();
return errorCode;
}
使用 VS 2015 在 Windows 上的输出是
The tree just created contains: 2 elements.
<?xml version="1.0" encoding="UTF-16" standalone="no" ?><svg:svg xmlns:svg="http://www.w3.org/2000/svg"><svg:circle/></svg:svg>
这样效果很好,不会出现您所说的片段问题。
我正在尝试通过 C++ 中的 xerces 创建 soap WSDL。我只想创建 XML 并将其放在一个文件中。
在创建 xml 时,我使用以下代码片段创建 xml 文档
pDomDocument = p_DOMImplementation->createDocument("http://schemas.xmlsoap.org/soap/envelope/",
convertStringToXmlCh("soapenv:Envelope"),
0);
但是这一行在 运行ning 时抛出异常。异常是:"Caught dom exception with message :invalid or illegal XML character".
此外,我的 xml 除了默认命名空间外还有三个命名空间。我如何通过 xerces 添加它。
但是我在 createDocument 函数中从根名称中删除冒号 (:) 的时间 运行。你能告诉我如何通过 xerces 将命名空间名称放入 xml 文档中吗?
我尝试在 google 上搜索了很多,但都没有成功。
编辑 1:
DOMImplementation* p_DOMImplementation = NULL;
p_DOMImplementation = DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("core"));
DOMDocument* pDomDocument=NULL;
try
{
pDomDocument = p_DOMImplementation->createDocument(convertStringToXmlCh(uri), //Root element namespace URI
convertStringToXmlCh(rootNode), // Root element name
0); //Document type (DTD)
}
catch(DOMException& e)
{
BL_LOG_MSG(BL_FATAL,"Caught dom exception with message :<1>",XMLString::transcode(e.getMessage()))
return NULL;
}
编辑 2:
下面是我的确切代码仍然出现异常:
int main()
{
DOMImplementation* p_DOMImplementation = NULL;
XMLPlatformUtils::Initialize();
p_DOMImplementation = DOMImplementationRegistry::getDOMImplementation(convertStringToXmlCh("Core"));
DOMDocument* pDomDocument=NULL;
try
{
pDomDocument = p_DOMImplementation->createDocument(convertStringToXmlCh("http://schemas.xmlsoap.org/soap/envelope/"), //Root element namespace URI
convertStringToXmlCh("soapenv:envelope"), // Root element name
NULL); //Document type (DTD)
}
catch(DOMException& e)
{
cout <<"Caught xception:"<<e.getMessage();
return NULL;
}
catch(...)
{
cout <<"Caught xception:";
return NULL;
}
if(pDomDocument == NULL)
{
return NULL;
cout<<"NULL returned";
}
DOMElement* pRootElement = pDomDocument->getDocumentElement();
addAttributeToDomElementNS(pRootElement,"xmlns:typ","http://abcd.fg/V1/types");
char* path = "/users/gen/app/mayank.xml";
SerializeXmlWriter(pDomDocument,path);
return 0;
}
低于异常:"Caught xception:invalid or illegal XML character"
这是一个基于 Xerces 示例 CreateDOMDocument
的示例,仅在命名空间中创建元素。该示例在 Window 10 上对我来说工作正常,针对 xerces-c-3.1.1-x86-windows-vc-10.0:
#include "stdafx.h"
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#if defined(XERCES_NEW_IOSTREAMS)
#include <iostream>
#else
#include <iostream.h>
#endif
XERCES_CPP_NAMESPACE_USE
// ---------------------------------------------------------------------------
// This is a simple class that lets us do easy (though not terribly efficient)
// trancoding of char* data to XMLCh data.
// ---------------------------------------------------------------------------
class XStr
{
public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
XStr(const char* const toTranscode)
{
// Call the private transcoding method
fUnicodeForm = XMLString::transcode(toTranscode);
}
~XStr()
{
XMLString::release(&fUnicodeForm);
}
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
const XMLCh* unicodeForm() const
{
return fUnicodeForm;
}
private:
// -----------------------------------------------------------------------
// Private data members
//
// fUnicodeForm
// This is the Unicode XMLCh format of the string.
// -----------------------------------------------------------------------
XMLCh* fUnicodeForm;
};
#define X(str) XStr(str).unicodeForm()
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
int main(int argC, char*[])
{
// 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;
}
// Watch for special case help request
int errorCode = 0;
if (argC > 1)
{
XERCES_STD_QUALIFIER cout << "\nUsage:\n"
" CreateDOMDocument\n\n"
"This program creates a new DOM document from scratch in memory.\n"
"It then prints the count of elements in the tree.\n"
<< XERCES_STD_QUALIFIER endl;
errorCode = 1;
}
if (errorCode) {
XMLPlatformUtils::Terminate();
return errorCode;
}
{
// Nest entire test in an inner block.
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));
if (impl != NULL)
{
try
{
DOMDocument* doc = impl->createDocument(
X("http://www.w3.org/2000/svg"), // root element namespace URI.
X("svg:svg"), // root element name
0); // document type object (DTD).
doc->getDocumentElement()->appendChild(doc->createElementNS(X("http://www.w3.org/2000/svg"), X("svg:circle")));
//
// Now count the number of elements in the above DOM tree.
//
const XMLSize_t elementCount = doc->getElementsByTagName(X("*"))->getLength();
XERCES_STD_QUALIFIER cout << "The tree just created contains: " << elementCount
<< " elements." << XERCES_STD_QUALIFIER endl;
DOMImplementationLS* lsImpl = (DOMImplementationLS*)impl;
DOMLSSerializer* ser = lsImpl->createLSSerializer();
XERCES_STD_QUALIFIER wcout << ser->writeToString(doc);
doc->release();
ser->release();
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
errorCode = 5;
}
catch (const DOMException& e)
{
XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
errorCode = 2;
}
catch (...)
{
XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
errorCode = 3;
}
} // (inpl != NULL)
else
{
XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
errorCode = 4;
}
}
XMLPlatformUtils::Terminate();
return errorCode;
}
使用 VS 2015 在 Windows 上的输出是
The tree just created contains: 2 elements.
<?xml version="1.0" encoding="UTF-16" standalone="no" ?><svg:svg xmlns:svg="http://www.w3.org/2000/svg"><svg:circle/></svg:svg>
这样效果很好,不会出现您所说的片段问题。