⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml.cpp

📁 xerces文件解析示例
💻 CPP
字号:
#include <string>
#include <iostream>
#include <sstream>
 
 
 
#include "XML.h"
 
CXML::CXML()
{
     try
     {   
         // Initialize Xerces-C++ library
         XMLPlatformUtils::Initialize();
     }
     catch(xercesc::XMLException & excp)  
     {
         char* msg = XMLString::transcode(excp.getMessage());
         printf("XML toolkit initialization error: %s\n", msg);
         XMLString::release(&msg);
     }
   
     m_DOMXmlParser = new XercesDOMParser;
}
CXML::~CXML()
{
     try
     {
         XMLPlatformUtils::Terminate();
     }
     catch(XMLException& excp)
     {
         char* msg = XMLString::transcode(excp.getMessage());
         printf("XML toolkit terminate error: %s\n", msg);
         XMLString::release(&msg);
     }
}


void CXML::xmlParser(string xmlFile) throw( std::runtime_error )
{
//获取文件信息状态
    
   
     //配置DOMParser
     m_DOMXmlParser->setValidationScheme( XercesDOMParser::Val_Auto );
     m_DOMXmlParser->setDoNamespaces( false );
     m_DOMXmlParser->setDoSchema( false );
     m_DOMXmlParser->setLoadExternalDTD( false );
   
     try
     {
         //调用 Xerces C++ 类库提供的解析接口
         m_DOMXmlParser->parse(xmlFile.c_str()) ;
        
         //获得DOM树
   DOMDocument* xmlDoc = m_DOMXmlParser->getDocument();
   DOMElement *pRoot = xmlDoc->getDocumentElement();
   if (!pRoot )
   {
    throw(std::runtime_error( "empty XML document" ));
   }
  
 //DOMNode* node = NULL;
  //DOMNode* node1 = NULL;
 //node = pRoot->getFirstChild();
  //node=node->getFirstChild();
 //node1=node->getFirstChild();

 // std::cout<<XMLString::transcode(node->getNodeName());  
      // create a walker to visit all text nodes.
   /**********************************************
   DOMTreeWalker *walker =
   xmlDoc->createTreeWalker(pRoot, DOMNodeFilter::SHOW_TEXT, NULL, true);
   // use the tree walker to print out the text nodes.
   std::cout<< "TreeWalker:\n";
  
     for (DOMNode *current = walker->nextNode(); current != 0; current = walker->nextNode() )
     {
   
    char *strValue = XMLString::transcode( current->getNodeValue() );
             std::cout <<strValue;
             XMLString::release(&strValue);
    }
    std::cout << std::endl;
  
   *************************************************/
  
   // create an iterator to visit all text nodes.
 DOMNodeIterator* iterator = xmlDoc->createNodeIterator(pRoot,DOMNodeFilter::SHOW_TEXT,   NULL, true);
  
   // use the tree walker to print out the text nodes.
  string var("Variable");
			 string oper("Operation");
			 string wd("Window");

  /* for ( DOMNode * current = iterator->nextNode();
   current != 0; current = iterator->nextNode() )
   {   //std::cout<<XMLString::transcode(current->getNodeName());  
	   std::cout<<XMLString::transcode(current->getNodeValue());  
        if (current->getNodeType() == DOMNode::ELEMENT_NODE)           
			std::cout<<"hajisafjao";
   
   }
  */
int i=0;
    //string var("Variable");
			 //string oper("Operation");
			// string wd("Window");
//	DOM_Text value;
			 DOMNode* node = NULL;
			 for (node = pRoot->getFirstChild();node != NULL;node = node->getNextSibling())
			 { 
				   if (node->getNodeType() == DOMNode::ELEMENT_NODE)
				   {std::cout<<"nodenameggggg_____"<<XMLString::transcode(node->getNodeName())<<std::endl;
				       // std::cout<<"______"<<XMLString::transcode(node->getNodeValue());
						if(node->getFirstChild()!=NULL)
							if(node->getFirstChild()->getNodeType() == DOMNode::TEXT_NODE)
                                	std::cout<<"______"<<XMLString::transcode(node->getFirstChild()->getNodeValue());
				   DOMNamedNodeMap *varAttributes;
 					   varAttributes = node->getAttributes();
 					   DOMNode *attribute1 = varAttributes->item (0);
					   string var = XMLString::transcode(attribute1->getNodeValue());
					   std::cout<<var;
					 
						 int attrCount;
						 DOMNode* attribute;
     attrCount = varAttributes->getLength ();
     for (i = 0; i < attrCount; i++)    {
         attribute = varAttributes->item (i);
		 std::cout<<XMLString::transcode(attribute->getNodeName())<<":"<<XMLString::transcode(attribute->getNodeValue());
        
     }


 					  // DOMNode *attribute2 = varAttributes->item (2);
 					  // string value = XMLString::transcode(attribute2->getNodeValue());
			 
					  // i++;
				   }
					

				    //if (node->getNodeType() == DOMNode::TEXT_NODE)
					//	std::cout<<"______"<<XMLString::transcode(node->getNodeValue());
				   /*if (node->getNodeType() == DOMNode::ELEMENT_NODE&&oper == XMLString::transcode(node->getNodeName()))
				   {std::cout<<"nodenameggggg_____"<<XMLString::transcode(node->getNodeName())<<std::endl;
					  
						for (DOMNode* node1 = node->getFirstChild();node1 != NULL;node1 = node1->getNextSibling())
						{
							if (node1->getNodeType() == DOMNode::ELEMENT_NODE)
							{
								for (DOMNode* node2 = node1->getFirstChild();node2 != NULL;node2 = node2->getNextSibling())
								{
									if (node2->getNodeType() == DOMNode::ELEMENT_NODE)
									{
										for (DOMNode* node3 = node2->getFirstChild();node3 != NULL;node3 = node3->getNextSibling())
										{
											if (node3->getNodeType() == DOMNode::ELEMENT_NODE)
											{
											//	string win = XMLString::transcode(node3->getNodeName());
											//	std::cout<<win<<std::endl;
												std::cout<<"nodename_____"<<XMLString::transcode(node3->getNodeName())<<std::endl;
												DOMNamedNodeMap *mesAttributes;
												mesAttributes = node3->getAttributes();
												DOMNode *attribute3 = mesAttributes->item (1);
												string var0 = XMLString::transcode(attribute3->getNodeValue());
										//		std::cout<<var0<<std::endl;
											 
											}
										}
									}
								}
							
							}
							
						}
				   }*/
			 }
   std::cout<< std::endl;
  
}
catch( xercesc::XMLException& excp )
{
   char* msg = xercesc::XMLString::transcode( excp.getMessage() );
   ostringstream errBuf;
   errBuf << "Error parsing file: " << msg << flush;
   XMLString::release( &msg );
}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -