libxml2库 读取xml文件,写xml文件

前端之家收集整理的这篇文章主要介绍了libxml2库 读取xml文件,写xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

所用xml文件内容

<?xml version="1.0" encoding="utf-8"?>
<style name="file">
  <contents name="type">
    <content>.mp3</content>
    <content>.mp4</content>
    <content>.wma</content>
    <content>.mkv</content>
    <content>.avi</content>
  </contents>
  <check>-2852</check>
</style>

XmlReader.h实现功能,得到一个xml文件中的全部内容以及效验xml文件的有效性!


#include <string>
#include <iostream>
#include <stdio.h>
#include <list>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>


class XmlReader
{
public:
    XmlReader();
    ~XmlReader();


    std::list<std::string> 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 <cstring>
#include <iostream>


using namespace std;

    XmlReader::XmlReader()
        : m_doc(NULL),m_node(NULL),m_nodeContent(NULL)
    {

    }
    XmlReader::~XmlReader()
    {

    }

    list<string> 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<string> 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 << "content:" << m_nodeContent << endl;
    int contentVal = checkMethod((const char*)m_nodeContent);

    //read check content
    while (NULL != m_node->next) {
        m_node = m_node->next;
    }
    m_nodeContent = xmlNodeGetContent(m_node);
    cout << "check val:" << m_nodeContent << endl;
    int checkVal = atoi((const char*)m_nodeContent);

    if (!(contentVal + checkVal)) {
        return true;
    }

    return false;

}

int XmlReader::checkMethod(const char* content)
{
    int length = strlen(content);
    int checkNum = 0;
    for (int i = 0; i < length; ++i)
    {
        /* code */
        checkNum += content[i];
    }

    return checkNum;
}


运行结果:

linux:~/workspace/xml/testxml$ g++ -o testxml.out testxml.cpp XmlReader.cpp -I/usr/include/libxml2/ -lxml2
linux:~/workspace/xml/testxml$ ./testxml.out 
.mp3
.mp4
.wma
.mkv
.avi
content:.mp3.mp4.wma.mkv.avi
check val:-2852
0
linux:~/workspace/xml/testxml$ 


写xml文件

XmlWriter.h文件。实现功能
fileTypeWrite函数向xml文件中加入新的节点,然后checkValWrite计算所有contents节点下面的字符的每个字节的和,之后取反。

这里用到的是xmlReadFile读取,而没有用xmlParseFile,因为前者在写入时,可以写入回车字符,在使用xmlSaveFormatFile保存文件时,注意第三个参数设置为1。

#include <string>
#include <iostream>
#include <stdio.h>
#include <list>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>


class XmlWriter
{
public:
    XmlWriter();
    ~XmlWriter();


    void fileTypeWrite(std::string filename,const char* type);
    bool checkValWrite(std::string filename);

private:
    int checkVal(const char* content);

private:
    xmlDocPtr m_doc;
    xmlNodePtr m_node;
    xmlChar* m_nodeContent;

};

XmlWriter.cpp文件内容
#include <cstring>
#include <iostream>
#include <stdlib.h>

#include "XmlWriter.h"

using namespace std;

    XmlWriter::XmlWriter()
        : m_doc(NULL),m_nodeContent(NULL)
    {

    }

    XmlWriter::~XmlWriter()
    {

    }

    void XmlWriter::fileTypeWrite(std::string filename,const char* type)
    {
        const char *docname = filename.c_str();
        //xmlKeepBlanksDefault(0);
        //m_doc = xmlParseFile(docname);
        m_doc = xmlReadFile(docname,NULL,XML_PARSE_NOBLANKS | XML_PARSE_NODICT);

        if(m_doc == NULL){
            fprintf(stderr,"empty document\n");
            xmlFreeDoc(m_doc);
        }

        m_node = m_node->xmlChildrenNode;

        xmlNodePtr nodePtr = NULL;
        nodePtr = xmlNewTextChild(m_node,(const xmlChar*)"content",(const xmlChar*)type);
        if (NULL == nodePtr) {
            fprintf(stderr,"add node Failed\n");
        }


        if (m_doc != NULL) {
            //xmlSaveFormatFile(filename,doc,0);
            xmlSaveFormatFile (filename.c_str(),m_doc,1);
            xmlFreeDoc(m_doc);
        }

    }

bool XmlWriter::checkValWrite(std::string filename)
{
    const char *docname = filename.c_str();
    //xmlKeepBlanksDefault(0);
    //m_doc = xmlParseFile(docname);
    m_doc = xmlReadFile(docname,XML_PARSE_NOBLANKS | XML_PARSE_NODICT);

    if(m_doc == NULL){
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(m_doc);
    }

    m_node = m_node->xmlChildrenNode;
    // while (NULL != m_node->next) {
    //     m_node = m_node->next;
    // }

    m_nodeContent = xmlNodeGetContent(m_node);
    cout << "content val:" << m_nodeContent << endl;
    int checkValue = -checkVal((const char*)m_nodeContent);
    char* chVal = new char[100];
    sprintf(chVal,"%d",checkValue);


    cout << chVal << endl;


cout << "===========================" << endl;

    m_node = xmlDocGetRootElement(m_doc);

    m_node = m_node->xmlChildrenNode;
    while (NULL != m_node->next) {
        m_node = m_node->next;
    }

    if ((!xmlStrcmp(m_node->name,(const xmlChar *)"check"))) {
        xmlNodeSetContent(m_node,(const xmlChar*)chVal);
    } else {
        m_node = xmlDocGetRootElement(m_doc);
        xmlNodePtr nodePtr = NULL;
        nodePtr = xmlNewTextChild(m_node,(const xmlChar*)"check",(const xmlChar*)chVal);
        if (NULL == nodePtr) {
            fprintf(stderr,"add check value Failed\n");
        }
    }



    delete []chVal;

    if (m_doc != NULL) {
        //xmlSaveFormatFile(docname,0);
        xmlSaveFormatFile (docname,1);
        xmlFreeDoc(m_doc);
    }

}

int XmlWriter::checkVal(const char* content)
{
    int length = strlen(content);
    int checkNum = 0;
    for (int i = 0; i < length; ++i)
    {
        /* code */
        checkNum += content[i];
    }

    return checkNum;
}


运行结果:

linux:~/workspace/xml/testxml$ g++ -o testxml.out testxml.cpp XmlWriter.cpp -I/usr/include/libxml2/ -lxml2
linux:~/workspace/xml/testxml$ ./testxml.out 
content val:.mp3.mp4.wma.mkv.avi.avi
-2120
===========================
linux:~/workspace/xml/testxml$ 

Write完之后的xml文件
<?xml version="1.0" encoding="utf-8"?>
<style name="file">
  <contents name="type">
    <content>.mp3</content>
    <content>.mp4</content>
    <content>.wma</content>
    <content>.mkv</content>
    <content>.avi</content>
    <content>.avi</content>
  </contents>
  <check>-2120</check>
</style>
原文链接:https://www.f2er.com/xml/294857.html

猜你在找的XML相关文章