本文整理汇总了C++中DOMDocument::createTreeWalker方法的典型用法代码示例。如果您正苦于以下问题:C++ DOMDocument::createTreeWalker方法的具体用法?C++ DOMDocument::createTreeWalker怎么用?C++ DOMDocument::createTreeWalker使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::createTreeWalker方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getWholeText
const XMLCh* DOMTextImpl::getWholeText() const
{
DOMDocument *doc = getOwnerDocument();
if (!doc) {
throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0, GetDOMNodeMemoryManager);
return 0;
}
DOMNode* root=doc->getDocumentElement();
DOMTreeWalker* pWalker=doc->createTreeWalker(root!=NULL?root:(DOMNode*)this, DOMNodeFilter::SHOW_ALL, NULL, true);
pWalker->setCurrentNode((DOMNode*)this);
// Logically-adjacent text nodes are Text or CDATASection nodes that can be visited sequentially in document order or in
// reversed document order without entering, exiting, or passing over Element, Comment, or ProcessingInstruction nodes.
DOMNode* prevNode;
while((prevNode=pWalker->previousNode())!=NULL)
{
if(prevNode->getNodeType()==ELEMENT_NODE || prevNode->getNodeType()==COMMENT_NODE || prevNode->getNodeType()==PROCESSING_INSTRUCTION_NODE)
break;
}
XMLBuffer buff(1023, GetDOMNodeMemoryManager);
DOMNode* nextNode;
while((nextNode=pWalker->nextNode())!=NULL)
{
if(nextNode->getNodeType()==ELEMENT_NODE || nextNode->getNodeType()==COMMENT_NODE || nextNode->getNodeType()==PROCESSING_INSTRUCTION_NODE)
break;
if(nextNode->getNodeType()==TEXT_NODE || nextNode->getNodeType()==CDATA_SECTION_NODE)
buff.append(nextNode->getNodeValue());
}
pWalker->release();
XMLCh* wholeString = (XMLCh*)((DOMDocumentImpl*)doc)->allocate((buff.getLen()+1) * sizeof(XMLCh));
XMLString::copyString(wholeString, buff.getRawBuffer());
return wholeString;
}
示例2: main
//.........这里部分代码省略.........
delete filter;
}
{
DOMNode* node = doc->getFirstChild();
UNUSED(node); // silence warning
unsigned long whatToShow = DOMNodeFilter::SHOW_COMMENT;
MyFilter* filter = new MyFilter(DOMNode::COMMENT_NODE);
DOMNodeIterator* iter = doc->createNodeIterator(root, whatToShow, filter, true);
TASSERT(iter->getWhatToShow() == 128);
TASSERT(iter->getExpandEntityReferences() == 1);
DOMNode* nd;
nd = iter->nextNode();
TASSERT(nd == comment);
nd = iter->nextNode();
TASSERT(nd == 0);
delete filter;
}
////////// TreeWalker Test Cases ////////////////
{
unsigned long whatToShow = DOMNodeFilter::SHOW_ALL;
DOMTreeWalker* tw = ((DOMDocumentTraversal*)doc)->createTreeWalker(doc, whatToShow, 0, true);
TASSERT(tw->getCurrentNode() == doc);
TASSERT(tw->firstChild() == root);
TASSERT(tw->nextSibling() == 0);
TASSERT(tw->lastChild() == comment);
TASSERT(tw->firstChild() == 0);
TASSERT(tw->lastChild() == 0);
TASSERT(tw->nextSibling() == 0);
TASSERT(tw->nextNode() == 0);
TASSERT(tw->previousSibling() == E13);
TASSERT(tw->previousNode() == E122);
TASSERT(tw->parentNode() == E12);
TASSERT(tw->firstChild() == textNode2);
TASSERT(tw->previousSibling() == 0);
TASSERT(tw->nextSibling() == E121);
TASSERT(tw->nextNode() == E122);
TASSERT(tw->parentNode() == E12);
TASSERT(tw->previousSibling() == E11);
TASSERT(tw->previousNode() == root);
TASSERT(tw->previousNode() == doc);
TASSERT(tw->previousNode() == 0);
TASSERT(tw->parentNode() == 0);
TASSERT(tw->getCurrentNode() == doc);
}
{
MyFilter mf(DOMNode::ELEMENT_NODE);
unsigned long whatToShow = DOMNodeFilter::SHOW_ALL;
DOMTreeWalker* tw = doc->createTreeWalker(root, whatToShow, &mf, true);
示例3: process_xml
bool XmlWorldParser::process_xml(const char* xmlFile) {
try {
XMLPlatformUtils::Initialize();
} catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n" << message << "\n";
XMLString::release(&message);
return 1;
}
XercesDOMParser* parser = new XercesDOMParser();
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true); // optional
/**
* Configure parser error handling
*/
ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
// ErrorHandler *errHandler =(ErrorHandler*) new DOMPrintErrorHandler();
parser->setErrorHandler(errHandler);
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 DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n" << message << "\n";
XMLString::release(&message);
return -1;
} catch (const SAXException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: " << message << "\n";
XMLString::release(&message);
return -1;
} catch (const exception& toCatch) {
cout << "Unexpected Exception \n" << toCatch.what() << endl;
return -1;
}
/**
* Walk through the document, adding bodies and joints in their relative frames
*/
DOMDocument* doc = parser->getDocument();
DOMTreeWalker* walker = doc->createTreeWalker(doc->getDocumentElement(),
DOMNodeFilter::SHOW_ELEMENT, new BodiesInWorld(), true);
/** Initial world frame */
double transform[3] = { 0, 0, 0 };
processNode(walker, transform);
//TODO Ensure that I am cleaning up everything I need to
/** Clean up no longer needed resources **/
doc->release();
delete errHandler;
return true;
}
示例4: replaceWholeText
DOMText* DOMTextImpl::replaceWholeText(const XMLCh* newText)
{
DOMDocument *doc = getOwnerDocument();
DOMTreeWalker* pWalker=doc->createTreeWalker(doc->getDocumentElement(), DOMNodeFilter::SHOW_ALL, NULL, true);
pWalker->setCurrentNode((DOMNode*)this);
// Logically-adjacent text nodes are Text or CDATASection nodes that can be visited sequentially in document order or in
// reversed document order without entering, exiting, or passing over Element, Comment, or ProcessingInstruction nodes.
DOMNode* pFirstTextNode=this;
DOMNode* prevNode;
while((prevNode=pWalker->previousNode())!=NULL)
{
if(prevNode->getNodeType()==ELEMENT_NODE || prevNode->getNodeType()==COMMENT_NODE || prevNode->getNodeType()==PROCESSING_INSTRUCTION_NODE)
break;
pFirstTextNode=prevNode;
}
// before doing any change we need to check if we are going to remove an entity reference that doesn't contain just text
DOMNode* pCurrentNode=pWalker->getCurrentNode();
DOMNode* nextNode;
while((nextNode=pWalker->nextNode())!=NULL)
{
if(nextNode->getNodeType()==ELEMENT_NODE || nextNode->getNodeType()==COMMENT_NODE || nextNode->getNodeType()==PROCESSING_INSTRUCTION_NODE)
break;
if(nextNode->getNodeType()==ENTITY_REFERENCE_NODE)
{
DOMTreeWalker* pInnerWalker=doc->createTreeWalker(nextNode, DOMNodeFilter::SHOW_ALL, NULL, true);
while(pInnerWalker->nextNode())
{
short nodeType=pInnerWalker->getCurrentNode()->getNodeType();
if(nodeType!=ENTITY_REFERENCE_NODE && nodeType!=TEXT_NODE && nodeType!=CDATA_SECTION_NODE)
throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);
}
pInnerWalker->release();
}
}
DOMText* retVal=NULL;
// if the first node in the chain is a text node, replace its content, otherwise create a new node
if(newText && *newText)
{
if(!castToNodeImpl(pFirstTextNode)->isReadOnly() && (pFirstTextNode->getNodeType()==TEXT_NODE || pFirstTextNode->getNodeType()==CDATA_SECTION_NODE))
{
pFirstTextNode->setNodeValue(newText);
retVal=(DOMText*)pFirstTextNode;
}
else
{
if(getNodeType()==TEXT_NODE)
retVal=doc->createTextNode(newText);
else
retVal=doc->createCDATASection(newText);
pFirstTextNode->getParentNode()->insertBefore(retVal, pFirstTextNode);
}
}
// now delete all the following text nodes
pWalker->setCurrentNode(pCurrentNode);
while((nextNode=pWalker->nextNode())!=NULL)
{
if(nextNode->getNodeType()==ELEMENT_NODE || nextNode->getNodeType()==COMMENT_NODE || nextNode->getNodeType()==PROCESSING_INSTRUCTION_NODE)
break;
if(nextNode!=retVal)
{
// keep the tree walker valid
pWalker->previousNode();
nextNode->getParentNode()->removeChild(nextNode);
nextNode->release();
}
}
pWalker->release();
return retVal;
}
示例5: main
//.........这里部分代码省略.........
DOMText* drugsDataVal = doc->createTextNode(X("codiene"));
drugsElem->appendChild(drugsDataVal);
// create drugs elem children
DOMElement* mixElem = doc->createElement(X("mix"));
DOMElement* spriteElem = doc->createElement(X("sprite"));
DOMElement* leanElem = doc->createElement(X("lean"));
// append drugs elem children
drugsElem->appendChild(mixElem);
drugsElem->appendChild(spriteElem);
drugsElem->appendChild(leanElem);
DOMElement* crimeElem = doc->createElement(X("crime"));
rootElem->appendChild(crimeElem);
DOMText* crimeDataVal = doc->createTextNode(X("187"));
crimeElem->appendChild(crimeDataVal);
//
// Now count the number of elements in the above DOM tree.
//
const XMLSize_t elementCount = doc->getElementsByTagName(X("*"))->getLength();
XERCES_STD_QUALIFIER cout << "The tree just created contains: " << elementCount
<< " elements." << XERCES_STD_QUALIFIER endl;
// create serializer instance
DOMLSSerializer *theSerializer = ((DOMImplementationLS*) impl)->createLSSerializer();
// create output instance
DOMLSOutput *theOutputDesc = ((DOMImplementationLS*) impl)->createLSOutput();
// referenced the following http://stackoverflow.com/questions/2897317/writing-xml-with-xerces-3-0-1-and-c-on-windows
// set output from serializer to stdout
XMLFormatTarget *myFormTarget;
myFormTarget = new StdOutFormatTarget();
theOutputDesc->setByteStream(myFormTarget);
// Make the output more human readable by inserting line feeds.
if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
// The end-of-line sequence of characters to be used in the XML being written out.
theSerializer->setNewLine(XMLString::transcode("\r\n"));
// output the DOM
theSerializer->write(doc, theOutputDesc);
cout << endl << endl << "*****************************************" << endl;
// Using DOMTreeWalker class to traverse tree
// create tree walker
DOMTreeWalker* walker = doc->createTreeWalker(rootElem,
DOMNodeFilter::SHOW_ELEMENT,
NULL,
true);
// create vector to hold Dom nodes
vector<DOMNode*> elem_vec;
// traverse tree using walker
for (DOMNode* current = walker->nextNode(); current != 0; current = walker->nextNode()) {
// store nodes int vector
elem_vec.push_back(current);
}
// sort function - alphabetically elements
std::sort(elem_vec.begin(), elem_vec.end(), compare_elem_alpha);
// iterate through sorted nodes
for (auto i : elem_vec) {
int x;
string text = string(x(i->getTextContent()));
cout << "Node Name: " << x(i->getNodeName()) << ((text != "") ? " |Text Content: " + text : "") << endl;
//get attr map
if (i->hasAttributes()) {
DOMNamedNodeMap *attr_map = i->getAttributes();
// get each node in the map
for (x = 0; x < attr_map->getLength(); x++) {
DOMNode *temp = attr_map->item(x);
cout << setw(5) << "" << "Attribute" << x + 1 << ": " << x(temp->getNodeName()) << " = "
<< x(temp->getNodeValue()) << endl;
}
}
}
// release doc resources
doc->release();
} catch (const OutOfMemoryException&) {
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
errorCode = 5;
} catch (const DOMException& e) {
XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl;
errorCode = 2;
} catch (...) {
XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl;
errorCode = 3;
}
}// (impl != NULL)
else {
XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl;
errorCode = 4;
}
}
示例6: readServerConfig
//.........这里部分代码省略.........
cout << "Exception message is: \n" << message << "\n";
XMLString::release(&message);
return false;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return false;
}
DOMDocument * domDocument = parser->getDocument();
removeBaseAttr(domDocument);
// get a serializer, an instance of DOMLSSerializer
XMLCh tempStr[3] = {chLatin_L, chLatin_S, chNull};
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
// set user specified output encoding
theOutputDesc->setEncoding(0);
XMLDOMErrorReporter * xmlDOMErrorReporter = new XMLDOMErrorReporter();
DOMConfiguration * serializerConfig = theSerializer->getDomConfig();
serializerConfig->setParameter(XMLUni::fgDOMErrorHandler, xmlDOMErrorReporter);
// set feature if the serializer supports the feature/mode
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, true))
serializerConfig->setParameter(XMLUni::fgDOMWRTSplitCdataSections, true);
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true))
serializerConfig->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, false))
serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, false);
if (serializerConfig->canSetParameter(XMLUni::fgDOMWRTBOM, false))
serializerConfig->setParameter(XMLUni::fgDOMWRTBOM, false);
/*
XMLFormatTarget * myFormTarget = new StdOutFormatTarget();
theOutputDesc->setByteStream(myFormTarget);
theSerializer->write(domDocument, theOutputDesc);
*/
MemBufFormatTarget * myFormTarget = new MemBufFormatTarget();
theOutputDesc->setByteStream(myFormTarget);
theSerializer->write(domDocument, theOutputDesc);
/*
XMLFormatTarget * myFormTarget2 = new StdOutFormatTarget();
theOutputDesc->setByteStream(myFormTarget2);
theSerializer->write(domDocument, theOutputDesc);
*/
MemBufInputSource * memInput = new MemBufInputSource(myFormTarget->getRawBuffer(), myFormTarget->getLen(), path.c_str());
XercesDOMParser * parser2 = new XercesDOMParser();
parser2->setDoNamespaces(true);
parser2->setDoXInclude(true);
// parser2->setValidationScheme(XercesDOMParser::Val_Always);
// parser2->setDoSchema(true);
// parser2->setValidationSchemaFullChecking(true);
parser2->setErrorHandler(xmlParseErrorReporter);
try {
parser2->parse(*memInput);
} catch (const XMLException & toCatch) {
char * message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n" << message << "\n";
XMLString::release(&message);
return false;
}
catch (const DOMException& toCatch) {
char * message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n" << message << "\n";
XMLString::release(&message);
return false;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return false;
}
DOMDocument * domDocument2 = parser2->getDocument();
XMLNodeFilter * xmlNodeFilter = new XMLNodeFilter();
DOMTreeWalker * domTreeWalker = domDocument->createTreeWalker(domDocument2, DOMNodeFilter::SHOW_ALL, xmlNodeFilter, true);
this->serverConfig = extractServerConfig(domDocument2, domTreeWalker);
domTreeWalker->release();
delete xmlNodeFilter;
delete xmlDOMErrorReporter;
delete xmlParseErrorReporter;
delete parser;
XMLPlatformUtils::Terminate();
return true;
}