tags:

views:

56

answers:

2

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>
</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.

+1  A: 

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">).

Jerry Coffin
In what sense is `"a\\b\\c\\d"` XML?
anon
It's not. It's a specifier for a "path" in the XML -- this example would look for something like `<a><b><c><d>text</d></c></b><a>` and return `text` (with, of course, the possibility that each of the outer tags might contain other "stuff" that it would ignore). Using the example in the original question, you'd search for `DUMMYROOT\\root1\\root2\\root3`, and it would return "STRINGTOSEARCH".
Jerry Coffin
Oh, OK. Just as a matter of interest, what was the reason you chose backslash as the path separator?
anon
@Neil: It was being used mostly by people who are accustomed to Windows, so I made an XML "Path" look as much like a DOS/Windows path as I could. It would be trivial to change it to something else (change the second parameter to `split` at the beginning of `extract`).
Jerry Coffin
I am still confused with, what is input here.
Avinash
`input` is a string containing the XML you want to search in.
Jerry Coffin
extract("<root>Job Started</root>","Job Started\\root",false); is this correct.
Avinash
sorry my mistake following seems to working for me,extract("<root>Job Started</root>","root",true);
Avinash
A: 

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.

Jay
Yes, I thought about it, but node based search will be a problem to me. like If I want to search <ROOT TAG><ELEMENT1><ELEMENT2>SEARCH STRING</ELEMENT2></ELEMENT1></ROOT TAG>
Avinash