开发者

XML search algorithm C++

开发者 https://www.devze.com 2023-01-11 13:28 出处:网络
I am trying to find out how to write XML search algorithm. Following is the my File <DUMMYROOT> <root>Job Started</root>

I am trying to find out how to write XML search algorithm.

Following is the my File

    <DUMMYROOT>
<root>Job Started</root>
<root>Job Running</root>
&开发者_运维问答lt;/DUMMYROOT>

and I want search string as <root>Job Started</root> I should be able to supply inner level of nodes as a search string like

<DUMMYROOT><root1><root2><root3>STRINGTOSEARCH</root3></root2></root1></DUMMYROOT>

and my file may not the complete XML when I am applying my Search Algorithm.


Here's something I wrote a few years ago that seems to fit reasonably well with what you're looking for (though make no mistake, it is kind of ugly, and if the XML is really badly formed, it might run into a problem).

template <class OutIt>
void split(string const &input, string const &sep, OutIt output) {
    size_t start = 0;
    size_t pos;
    do { 
        pos = input.find(sep, start);
        std::string temp(input, start, pos-start);
        *output++ = temp;
        start = pos+1;
    } while (pos != string::npos);
}

string extract(string const &input, string const &field, bool whole=false) { 
    std::vector<std::string> names;
    split(field, "\\", std::back_inserter(names));

    size_t b = 0, e = string::npos;
    std::string ret(input);

    for (size_t i=0; i<names.size(); i++) {
        ret = std::string(ret, b, e-b);
        string sname  = "<" + names[i];
        string ename = "</" + names[i];
        if (whole) {
            sname+=">";
            ename+=">";
        }
        b = ret.find(sname);
        if (b==string::npos)
            return "";
        b = ret.find(">", b)+1;
        e = ret.find(ename, b);
        if (b==string::npos || e == string::npos)
            return "";
    }
    ret = std::string(ret, b, e-b);

    int pos;

    // minor cleanup: remove tabs from string before returning.        
    while ((pos=ret.find("\t"))!=std::string::npos)
        ret[pos] = ' ';

    return ret;
}

Normal use would be something like:

result = extract(input, "a\\b\\c\\d");

The "whole" parameter governs whether you've specified the "whole" tag, or whether it's allowed to have attributes in addition to what you've specified (e.g., <tag> vs. <tag attribute = "value">).


If your file is incomplete then most xml parsers will choke when trying to read it. You might be better off just doing a string search against the file content.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号