代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
XERCES_CPP_NAMESPACE_USE;
using namespace std;
string xml_string =
"奥巴马student";
DOMImplementation* xqillaImplementation = NULL;
void getpathvalue(DOMDocument* d)
{
const char* expression = "data(/corporation/node/@id)";
AutoRelease
resolver(d->createNSResolver(d->getDocumentElement()));
AutoRelease
parsedExpression(d->createExpression(X(expression), resolver));
AutoRelease
Result(parsedExpression->evaluate(
d,
DOMXPathResult::ITERATOR_RESULT_TYPE, 0));
if (Result->iterateNext())
{
AutoRelease
serializer(xqillaImplementation->createLSSerializer());
AutoRelease
output(xqillaImplementation->createLSOutput());
MemBufFormatTarget* target = new MemBufFormatTarget();
output->setByteStream(target);
if(Result->isNode())
{
cout << "Result: "<< flush;
serializer->write(Result->getNodeValue(), output);
cout<< target->getRawBuffer()<< endl;
}
else
{
std::cout
<< XMLString::transcode(Result->getStringValue())
<< std::endl;
}
}
else
{
std::cout<< "NULL"<< std::endl;
}
}
int main(int argc, char *argv[])
{
try {
XQillaPlatformUtils::initialize();
} catch (const XMLException& eXerces) {
cerr << "Error during Xerces-C initialisation.\n"
<< "Xerces exception message: "
<< UTF8(eXerces.getMessage()) << endl;
return 1;
}
try {
xqillaImplementation =
DOMImplementationRegistry::getDOMImplementation(X("LS"));
//part1: 解析新组建树的例子
DOMDocument* doc = xqillaImplementation->createDocument(
0,
X("corporation"),
0);
DOMElement* root_elem = doc->getDocumentElement();
DOMElement* node = doc->createElement(X("node"));
node->setTextContent(X("K L"));
node->setAttribute(X("id"), X("10005"));
root_elem->appendChild(node);
getpathvalue( doc );
//
//part2: 解析内存字符串的例子
AutoRelease parser(
xqillaImplementation->createLSParser(
DOMImplementationLS::MODE_SYNCHRONOUS, 0));
parser->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, false);
parser->getDomConfig()->setParameter(XMLUni::fgXercesSchema, false);
parser->getDomConfig()->setParameter(XMLUni::fgDOMValidateIfSchema, false);
MemBufInputSource* xml_mem = new MemBufInputSource(
(const XMLByte* )xml_string.c_str(),
strlen(xml_string.c_str()),
"xml",
false);
DOMDocument* document =
parser->parse( new Wrapper4InputSource(xml_mem) );
getpathvalue( document );
//
}
catch(DOMException &e) {
cerr << "DOMException: " << UTF8(e.getMessage()) << endl;
return 1;
}
XQillaPlatformUtils::terminate();
return 0;
}
阅读(2911) | 评论(0) | 转发(0) |