我正在尝试使用 Xerces DOMDocument C++ 解析器加载 HTML 并搜索特定的 HTML 元素。我很难找到如何实现这一点的好例子。我似乎发现的只是解析 XML。有人可以帮忙吗?谢谢。
问问题
993 次
1 回答
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");
于 2013-01-21T15:07:48.197 回答