libxml2库 读取xml文件,写xml文件 | 您所在的位置:网站首页 › libxml2丢失 › libxml2库 读取xml文件,写xml文件 |
所用xml文件内容: .mp3 .mp4 .wma .mkv .avi -2852XmlReader.h实现功能,得到一个xml文件中的全部内容以及效验xml文件的有效性! #include #include #include #include #include #include class XmlReader { public: XmlReader(); ~XmlReader(); std::list getAllContent(std::string filename); bool checkXml(std::string filename); private: int checkMethod(const char* content); private: xmlDocPtr m_doc; xmlNodePtr m_node; xmlChar* m_nodeContent; };
XmlReader.cpp内容: #include "XmlReader.h" #include #include using namespace std; XmlReader::XmlReader() : m_doc(NULL) , m_node(NULL) , m_nodeContent(NULL) { } XmlReader::~XmlReader() { } list XmlReader::getAllContent(std::string filename) { const char *docname = filename.c_str(); xmlKeepBlanksDefault(0); m_doc = xmlParseFile(docname); if(m_doc == NULL){ fprintf(stderr, "Document not parse successfully. \n"); } /* obtain root node */ m_node = xmlDocGetRootElement(m_doc); if(m_node == NULL){ fprintf(stderr, "empty document\n"); xmlFreeDoc(m_doc); } m_node = m_node->xmlChildrenNode; m_node = m_node->xmlChildrenNode; list fileType; char* type; while (NULL != m_node) { type = (char*)xmlNodeGetContent(m_node); string strType(type); fileType.push_back(strType); m_node = m_node->next; } return fileType; } bool XmlReader::checkXml(std::string filename) { const char *docname = filename.c_str(); xmlKeepBlanksDefault(0); m_doc = xmlParseFile(docname); if(m_doc == NULL){ fprintf(stderr, "Document not parse successfully. \n"); } /* obtain root node */ m_node = xmlDocGetRootElement(m_doc); if(m_node == NULL){ fprintf(stderr, "empty document\n"); xmlFreeDoc(m_doc); } //read xml file content m_node = m_node->xmlChildrenNode; m_nodeContent = xmlNodeGetContent(m_node); cout next; // } m_nodeContent = xmlNodeGetContent(m_node); cout |
CopyRight 2018-2019 实验室设备网 版权所有 |