Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536082
  • 博文数量: 55
  • 博客积分: 1520
  • 博客等级: 上尉
  • 技术积分: 661
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-19 22:01
文章存档

2013年(1)

2012年(1)

2011年(7)

2010年(16)

2009年(30)

我的朋友

分类: C/C++

2010-03-08 12:55:20

代码如下:
#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) |
给主人留下些什么吧!~~