C ++ Xerces Parser加载HTML并搜索HTML元素

时间:2013-01-21 15:01:29

标签: c++ html domdocument xerces

我尝试使用Xerces DOMDocument C ++ Parser加载HTML并搜索特定的HTML元素。我很难找到如何实现这一目标的好例子。我似乎发现的只是解析XML。有人可以帮忙吗?感谢。

1 个答案:

答案 0 :(得分:1)

看看这个:http://xerces.apache.org/xerces-c/program-dom-3.html

DOMDocument也有一个例子:

//     //创建一个小文档树     //

{
    XMLCh tempStr[100];

    XMLString::transcode("Range", tempStr, 99);
    DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr, 0);

    XMLString::transcode("root", tempStr, 99);
    DOMDocument*   doc = impl->createDocument(0, tempStr, 0);
    DOMElement*   root = doc->getDocumentElement();

    XMLString::transcode("FirstElement", tempStr, 99);
    DOMElement*   e1 = doc->createElement(tempStr);
    root->appendChild(e1);

    XMLString::transcode("SecondElement", tempStr, 99);
    DOMElement*   e2 = doc->createElement(tempStr);
    root->appendChild(e2);

    XMLString::transcode("aTextNode", tempStr, 99);
    DOMText*       textNode = doc->createTextNode(tempStr);
    e1->appendChild(textNode);

    // optionally, call release() to release the resource associated with the range after done
    DOMRange* range = doc->createRange();
    range->release();

    // removedElement is an orphaned node, optionally call release() to release associated resource
    DOMElement* removedElement = root->removeChild(e2);
    removedElement->release();

    // no need to release this returned object which is owned by implementation
    XMLString::transcode("*", tempStr, 99);
    DOMNodeList*    nodeList = doc->getElementsByTagName(tempStr);

    // done with the document, must call release() to release the entire document resources
    doc->release();
};

......等等。

编辑:

  

但是如何将HTML加载到DOMDocument中并搜索html元素?   这就是我想弄清楚的事情。

XercesDOMParser解析器;

parser.loadGrammar(“grammar.dtd”,Grammar :: DTDGrammarType);

parser.setValidationScheme(XercesDOMParser :: Val_Always);

处理程序处理程序;

parser.setErrorHandler(& handler);

parser.parse( “xmlfile.xml”);