XML的个人习惯解析方法总结


XML文件的解析方法
// XMLparse.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include     // 正则表达式的头文件
#include "tinyxml.h"    // 包含TinyXML类库
using namespace std;
#import "msxml6.dll"    // com的dll文件
#ifdef UNICODE
#define FILEPATH FILEPATHW
#define FILEPATHW _T("C:/Users/Administrator/Desktop/network.xml")
#else
#define FILEPATH FILEPATHA
#defien FILEPATHA ("C:/Users/Administrator/Desktop/network.xml")
#endif

TCHAR g_filePath[] = FILEPATH;
using namespace MSXML2;
/*******利用TinyXML进行解析(最佳)*****/
void test4(){
    TiXmlDocument doc("C:/Users/Administrator/Desktop/network.xml");
    bool loadResult = doc.LoadFile();
    if (!loadResult){
        printf("Load XML file error\n");
        return;
    }
    // 加载成功
    TiXmlElement *root = doc.RootElement();
    for (TiXmlNode * item = root->FirstChild("Ftp"); item;item = item->NextSibling("Ftp"))
    {
        TiXmlNode*child = item->FirstChild();
        const char*host = child->ToElement()->GetText();
        printf("host - %s", host);
        child = child->NextSiblingElement();
        const char* port = child->ToElement()->GetText();
        printf("\t port: %s \n", port);
        child = child->NextSiblingElement();
        const char* username = child->ToElement()->GetText();
        printf("\t username: %s \n", username);
        child = child->NextSiblingElement();
        const char* password = child->ToElement()->GetText();
        printf("\t password: %s \n", password);
        printf("----------\n\n");
    }
}
//利用TinyXML创建XML文件
void test41()
{
    //创建一个文档对象
    TiXmlDocument * myDocument = new TiXmlDocument();
    //创建一个根节点
    TiXmlElement *rootElement = new TiXmlElement("TBxml");
    myDocument->LinkEndChild(rootElement);
    //创建一个子节点
    TiXmlElement * test1 = new TiXmlElement("test1");
    rootElement->LinkEndChild(test1);
    TiXmlElement * test2 = new TiXmlElement("test1");
    rootElement->LinkEndChild(test2);

    myDocument->SaveFile("C:/Users/Administrator/Desktop/TB.xml");
}


/*********利用正则表达式解析XML************/
void test3(){
    HANDLE hFile = CreateFileW(g_filePath, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    DWORD dwSize;
    DWORD dwRead;
    // 获取文件大小
    dwSize = GetFileSize(hFile, 0);
    char * buf = new char[dwSize + 1];
    // 动态的申请一段内存
    memset(buf, 0, dwSize + 1); // 加一表示‘\0’结尾
    // 把文件内容读取到内存中
    ReadFile(hFile, buf, dwSize, &dwRead, NULL);

    regex_constants::syntax_option_type f1 = regex_constants::icase;/*取消大小写敏感*/
    regex pattern(".*", f1);
    string sBuf = buf;
    tr1::sregex_token_iterator ite(sBuf.begin(), sBuf.end(), pattern), end;
    for (; ite != end;++ite)
    {
        printf("%s\n", ite->str().c_str());
    }
}

/************利用com组件进行XML解析****************/
//自定义函数(用于COM组件解析XML)
void ReadElement(MSXML2::IXMLDOMNodeListPtr nodelist){
    int iLength = nodelist->Getlength();
    for (int nodeCount = 0; nodeCount < iLength;nodeCount++){
        // 获取子节点信息
        MSXML2::IXMLDOMNodePtr nodePtr = nodelist->nextNode();
        _variant_t strNodeName = nodePtr->GetnodeName();
        _variant_t strNodeValue = nodePtr->GetnodeValue();
        _variant_t strNodeText = nodePtr->Gettext();
        wprintf(L"\tNode Name: %s\tNode Value: %s\tNode Text: %s\n",strNodeName.bstrVal, strNodeValue.bstrVal, strNodeText.bstrVal);
        // 获取子节点的属性
        MSXML2::IXMLDOMNamedNodeMapPtr pDomAttrlist = nodePtr->Getattributes();
        if (pDomAttrlist){
            for (int j = 0; j < pDomAttrlist->Getlength();j++)
            {
                MSXML2::IXMLDOMNodePtr pDomAttr = pDomAttrlist->Getitem(j);
                wprintf(L"Node's attribute: %s--%s\n", (LPCWSTR)pDomAttr->GetnodeName(), (LPCWSTR)pDomAttr->Gettext());
            }
        }
        ReadElement(nodePtr->GetchildNodes());
    }
}

// 
void test2(){
    MSXML2::IXMLDOMDocumentPtr docPtr;
    MSXML2::IXMLDOMNodePtr DomNodePtr;
    MSXML2::IXMLDOMNamedNodeMapPtr DomNamedNodeMapPtr;
    // com组件初始化操作
    CoInitialize(0);
    docPtr.CreateInstance("msxml2.domdocument");
    // 打开一个XML文件
    _variant_t varXml(g_filePath);
    _variant_t varOut((bool)TRUE);
    varOut = docPtr->load(varXml);
    if ((bool)varOut == FALSE)
    {
        printf("Open Xml file error\n");
        return;
    }
    // 获取根节点
    MSXML2::IXMLDOMElementPtr root = docPtr->documentElement;
    // 获取根节点的文字
    _variant_t strRootName = root->nodeName;
    wprintf(L"Root name:%s\n", strRootName.bstrVal);
    // 
    ReadElement(root->GetchildNodes());
}

/************手工解析XML(复杂,易错)***************/
 //
void test1(){
    HANDLE hFile = CreateFileW(g_filePath, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    DWORD dwSize;
    DWORD dwRead;
    // 获取文件大小
    dwSize = GetFileSize(hFile, 0);
    char * buf = new char[dwSize + 1];
    // 动态的申请一段内存
    memset(buf, 0, dwSize + 1); // 加一表示‘\0’结尾
    // 把文件内容读取到内存中
    ReadFile(hFile, buf, dwSize, &dwRead, NULL);
    // 创建文档的首尾指针、
    char* lpStart, *lpEnd;
    lpStart = buf;
    lpEnd = buf + dwSize;
    for (; lpStart < lpEnd; lpStart++){
        if (memcmp(lpStart, "", 5) == 0){
            lpStart += 5;
            break;
        }
    }
    char* lpStart2 = lpStart;
    for (; lpStart2 < lpEnd;lpStart2++){
        if (memcmp(lpStart2,"",6) == 0){
            *lpStart2 = 0;
            break;
        }
    }
    printf("%s", lpStart);
    delete []buf;
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //test1();
    //test2();
    //test3();
    test41();
    system("pause");
    return 0;
}

你可能感兴趣的:(杂文)