用XQilla根据XPATH解析XML
时间:2010-03-08 来源:huabo_linux
代码如下:
#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/Wrapper4InputSource.hpp>
#include <xqilla/xqilla-dom3.hpp>
XERCES_CPP_NAMESPACE_USE;
using namespace std;
string xml_string =
"<corporation><node id='10000'>奥巴马</node><node id='10001'>student</node></corporation>";
DOMImplementation* xqillaImplementation = NULL;
void getpathvalue(DOMDocument* d)
{
const char* expression = "data(/corporation/node/@id)";
AutoRelease<DOMXPathNSResolver>
resolver(d->createNSResolver(d->getDocumentElement()));
AutoRelease<DOMXPathExpression>
parsedExpression(d->createExpression(X(expression), resolver));
AutoRelease<DOMXPathResult>
Result(parsedExpression->evaluate(
d,
DOMXPathResult::ITERATOR_RESULT_TYPE, 0));
if (Result->iterateNext())
{
AutoRelease<DOMLSSerializer>
serializer(xqillaImplementation->createLSSerializer());
AutoRelease<DOMLSOutput>
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<DOMLSParser> 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;
}
#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/Wrapper4InputSource.hpp>
#include <xqilla/xqilla-dom3.hpp>
XERCES_CPP_NAMESPACE_USE;
using namespace std;
string xml_string =
"<corporation><node id='10000'>奥巴马</node><node id='10001'>student</node></corporation>";
DOMImplementation* xqillaImplementation = NULL;
void getpathvalue(DOMDocument* d)
{
const char* expression = "data(/corporation/node/@id)";
AutoRelease<DOMXPathNSResolver>
resolver(d->createNSResolver(d->getDocumentElement()));
AutoRelease<DOMXPathExpression>
parsedExpression(d->createExpression(X(expression), resolver));
AutoRelease<DOMXPathResult>
Result(parsedExpression->evaluate(
d,
DOMXPathResult::ITERATOR_RESULT_TYPE, 0));
if (Result->iterateNext())
{
AutoRelease<DOMLSSerializer>
serializer(xqillaImplementation->createLSSerializer());
AutoRelease<DOMLSOutput>
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<DOMLSParser> 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;
}
相关阅读 更多 +