使用xerces读取xml属性的值

时间:2013-07-22 05:58:31

标签: c++ xml xml-parsing xerces

我有一个C ++程序,它使用xerces库将数据写入xml文件。任何人都可以帮助我使用相同的xerces库解析(读取)xml文件中的属性值吗?

我得到了以下用于此目的的程序:

#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/util/XMLString.hpp>
#include <conio.h>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
using namespace std;
using namespace xercesc;


class ErnstSax2Handler : public DefaultHandler 
{
    public:
    ErnstSax2Handler(void);
    virtual ~ErnstSax2Handler(void);

    void startElement(
    const   XMLCh* const    uri,
    const   XMLCh* const    localname,
    const   XMLCh* const    qname,
    const   Attributes&     attrs
    );
    void endElement(
    const   XMLCh* const    uri,
    const   XMLCh* const    localname,
    const   XMLCh* const    qname
    );
    void characters(
    const   XMLCh* const    chars,
    const   XMLSize_t       length
    );

    void fatalError(const SAXParseException&);

    protected:
    // define variables to save the state
};

void ErnstSax2Handler::startElement(const   XMLCh* const    uri,
                    const   XMLCh* const    localname,
                    const   XMLCh* const    qname,
                    const   Attributes&     attrs)
{
    char* name = XMLString::transcode(localname);
    cout<<name;
    // ...
    XMLString::release(&name);
}
void ErnstSax2Handler::endElement(
            const   XMLCh* const    uri,
            const   XMLCh* const    localname,
            const   XMLCh* const    qname)
{
    char* name = XMLString::transcode(localname);

    //...
    XMLString::release(&name);
}
void ErnstSax2Handler::fatalError(const SAXParseException& exception)
{
    char* message = XMLString::transcode(exception.getMessage());
    cout << "Fatal Error: " << message
         << " at line: " << exception.getLineNumber()
         << endl;
    XMLString::release(&message);
}
void ErnstSax2Handler::characters(
const   XMLCh* const    chars,
const   XMLSize_t       length
)
{
 // This is called when the parser is reading text.
 // You will need to save what state you are in via
 // startElement / endElement.
 }


int main () {

    try {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Error during initialization! :\n";
        cout << "Exception message is: \n"
         << message << "\n";
        XMLString::release(&message);
        return 1;
    }

    char* xmlFile = "test.xml";
    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optional

    DefaultHandler* defaultHandler = new DefaultHandler();
    xercesc::ContentHandler* h  = new DefaultHandler();

    parser->setContentHandler(h);
    parser->setErrorHandler(defaultHandler);

    try {
        parser->parse(xmlFile);
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"
         << message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (const SAXParseException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n"
             << message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (...) {
        cout << "Unexpected Exception \n" ;
        return -1;
    }    


//getch();
delete parser;
delete defaultHandler;
return 0;
}

但最终得到了很多这样的错误:

错误LNK2019:未解析的外部符号“__declspec(dllimport)public:static void __cdecl xercesc_3_1 :: XMLString :: release(char * *,class xercesc_3_1 :: MemoryManager * const)”(_ imp ?release @ XMLString @ xercesc_3_1 @@ SAXPAPADQAVMemoryManager @ 2 @@ Z)在函数“public:virtual void __thiscall ErnstSax2Handler :: startElement(wchar_t const * const,wchar_t const * const,wchar_t const * const,class xercesc_3_1 :: Attributes const)中引用&amp;)“(?startElement @ ErnstSax2Handler @@ UAEXQB_W00ABVAttributes @ xercesc_3_1 @@@ Z)XmlRead.obj 错误LNK2019:未解析的外部符号“__declspec(dllimport)public:static void __cdecl xercesc_3_1 :: XMLString :: release(char * *,class xercesc_3_1 :: MemoryManager * const)”(_ imp ?release @ XMLString @ xercesc_3_1 @@ SAXPAPADQAVMemoryManager @ 2 @@ Z)在函数“public:virtual void __thiscall ErnstSax2Handler :: startElement(wchar_t const * const,wchar_t const * const,wchar_t const * const,class xercesc_3_1 :: Attributes const&amp;)中引用“(?startElement @ ErnstSax2Handler @@ UAEXQB_W00ABVAttributes @ xercesc_3_1 @@@ Z)XmlRead.obj

有人可以向我推荐一个不同的程序或修复上述程序吗?

1 个答案:

答案 0 :(得分:0)

使用Xerces Sax Parser时遇到了同样的错误。 我能够通过正确指定其他依赖项来解决此问题。 您需要将Xerces(包括库和标题)指定为项目中的其他依赖项。这有望解决问题..

同样在您的代码中,您创建了默认处理程序,即

   DefaultHandler* defaultHandler = new DefaultHandler();
    xercesc::ContentHandler* h  = new DefaultHandler();

相反,您必须创建处理程序的实例,即

   DefaultHandler* defaultHandler = new ErnstSax2Handler();
    xercesc::ContentHandler* h  = new ErnstSax2Handler();

只要解析器遇到Start标记或内容标记或结束标记(或任何其他标记),就会创建处理程序的实例并执行处理程序中的函数。

有用的链接:http://www.onlamp.com/pub/a/onlamp/2005/11/10/xerces_sax.html