📄 cxml.cpp
字号:
#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include "CXML.hpp"
CXML::CXML()
{
try
{
XMLPlatformUtils::Initialize() ;
}
catch(xercesc::XMLException &e)
{
char *msg = XMLString::transcode(e.getMessage()) ;
printf("XML tooklit initialization error : %s\n" , msg) ;
XMLString::release(&msg) ;
}
dom_xml_parser = new XercesDOMParser ;
/*configue the DOMParser*/
dom_xml_parser->setValidationScheme(XercesDOMParser::Val_Auto);
dom_xml_parser->setDoNamespaces(false) ;
dom_xml_parser->setDoSchema(false) ;
dom_xml_parser->setLoadExternalDTD(false) ;
}
CXML::~CXML()
{
try
{
XMLPlatformUtils::Terminate() ;
}
catch(XMLException &e)
{
char *msg = XMLString::transcode(e.getMessage()) ;
printf("XML toolkit terminate error : %s\n" , msg);
XMLString::release(&msg) ;
}
}
void CXML::xmlParser(string &xmlFile)throw(std::runtime_error)
{
try
{
dom_xml_parser->parse(xmlFile.c_str()) ;
/*obtain the DOM tree(own the entire XML file) */
DOMDocument *xmlDoc = dom_xml_parser->getDocument() ;
/*get the chile node of the root element*/
DOMElement *pRoot = xmlDoc->getDocumentElement() ;
if (!pRoot)
{
throw(std::runtime_error("empty XML document"));
}
/*create a walker to visit all text nodes */
/**
* <code>DOMTreeWalker</code> objects are used to navigate a document tree or
* subtree using the view of the document defined by their
* <code>whatToShow</code> flags and filter (if any). Any function which
* performs navigation using a <code>DOMTreeWalker</code> will automatically
* support any view defined by a <code>DOMTreeWalker</code>.
*/
DOMTreeWalker *walker = xmlDoc->createTreeWalker(pRoot , DOMNodeFilter::SHOW_TEXT , NULL ,true) ;
std::cout << "TreeWalker :\n" ;
int i = 1 ;
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 ;
}
catch(xercesc::XMLException &e)
{
char *msg = xercesc::XMLString::transcode(e.getMessage());
ostringstream errBuf;
errBuf << "Error parsing file: " << msg << flush;
XMLString::release(&msg);
}
}
void CXML::getAttribute(string &xmlFile)
{
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
DOMLSParser *parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMConfiguration *config = parser->getDomConfig();
DOMDocument *doc = 0;
config->setParameter(XMLUni::fgDOMNamespaces, false);
config->setParameter(XMLUni::fgXercesSchema, false);
config->setParameter(XMLUni::fgXercesSchemaFullChecking, false);
config->setParameter(XMLUni::fgDOMValidate, true);
// enable datatype normalization - default is off
config->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
// And create our error handler and install it
try
{
/* reset document pool */
parser->resetDocumentPool();
doc = parser->parseURI(xmlFile.c_str());
}
catch(const XMLException& toCatch)
{
XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << xmlFile << "'\n"
<< "Exception message is: \n"
<< XMLString::transcode(toCatch.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
}
catch(...)
{
XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during parsing: '" << xmlFile << "'\n";
}
/*DOMNode is the parent of the DOMElement*/
DOMNode *myNode =(DOMNode*)doc->getDocumentElement();
disAttributes(myNode);
}
void CXML::disAttributes(DOMNode *myNode)
{
DOMNode *child;
if (myNode)
{
if (myNode->getNodeType() == DOMNode::ELEMENT_NODE)
{
char *name = XMLString::transcode(myNode->getNodeName());
XERCES_STD_QUALIFIER cout <<"Element : "<< name << XERCES_STD_QUALIFIER endl;
XMLString::release(&name);
if (myNode->hasAttributes())
{
DOMNamedNodeMap *attr = myNode->getAttributes() ;
const XMLSize_t nsize = attr->getLength() ;
XERCES_STD_QUALIFIER cout <<"\tAttributes :" << XERCES_STD_QUALIFIER endl;
for (XMLSize_t i = 0 ; i < nsize ; i++)
{
/*DOMNode is the parent of parent of the DOMAttr */
DOMAttr *nodeAttr = (DOMAttr*)attr->item(i) ;
char *attrName = XMLString::transcode(nodeAttr->getName());
XERCES_STD_QUALIFIER cout << "\t" << attrName << "=";
XMLString::release(&attrName);
attrName = XMLString::transcode(nodeAttr->getValue());
XERCES_STD_QUALIFIER cout << attrName << XERCES_STD_QUALIFIER endl;
XMLString::release(&attrName);
}
}
}
}
for (child = myNode->getFirstChild(); child != 0; child=child->getNextSibling())
disAttributes(child);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -