Thread: xaml filter data parser library c/c++

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    6

    xaml filter data parser library c/c++

    Hello everyone,
    It's my first post here, so a extra thank you for you time and understanding.

    I was trying to develop a solution to read .xaml file(code) and filter some tags and check the tags values.

    For instance, using the bellow code, how can I get all 'variable' tags and check if is 'String'and check if the value contains come specific keyword (using c/c++)?

    On another forum it was suggested a parser library but I did not found any information specifically for xaml, also the user could not provide information about the libraries.

    Thank you
    <Flowchart.Variables>
    <Variablex:TypeArguments="x:String"Name="strDebugDt"/>
    <Variablex:TypeArguments="x:String"Name="strMailFilter"/>
    <Variablex:TypeArguments="sd:DataTable"Name="dtReportAttachments"/>
    </Flowchart.Variables>

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    How predictable is the structure of the XAML files? If it could be just about anything, then you'll have a hard time writing a robust solution yourself parsing of a full grammar is a difficult undertaking. A quick websearch didn't return any XAML parsers in C/C++. Regex doesn't work well with languages of this type, so I think that's out the window.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    927
    XAML is valid XML, so you can use XPath to extract the information you want. I've never used an XML parsing library with XPath in C++ before, but here's what Google has to say:

    - GitHub - zeux/pugixml: Light-weight, simple and fast XML parser for C++ with XPath support
    - Libxml2 set of examples
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    After searching a lot and not finding information about xaml and c++ I did start looking also to xml, but didn't know if the libraries where the same... I will try your suggestions, thank you!

  5. #5
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    Quote Originally Posted by CodeMonkey View Post
    XAML is valid XML, so you can use XPath to extract the information you want. I've never used an XML parsing library with XPath in C++ before, but here's what Google has to say:

    - GitHub - zeux/pugixml: Light-weight, simple and fast XML parser for C++ with XPath support
    - Libxml2 set of examples
    After searching a lot and not finding information about xaml and c++ I did start looking also to xml, but didn't know if the libraries where the same... I will try your suggestions, thank you!

  6. #6
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    Quote Originally Posted by -Adrian View Post
    How predictable is the structure of the XAML files? If it could be just about anything, then you'll have a hard time writing a robust solution yourself – parsing of a full grammar is a difficult undertaking. A quick websearch didn't return any XAML parsers in C/C++. Regex doesn't work well with languages of this type, so I think that's out the window.
    What is considered predictable? I do know what tags I need to find, but everything else is not regular. I do know it's possible to do it because I know about similar projects but using other languages.

  7. #7
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Quote Originally Posted by JazzDev View Post
    What is considered predictable?
    If the files were always just:
    Code:
    <Flowchart.Variables>
    <Variablex:TypeArguments="x:String"Name="strDebugDt"/>
    <Variablex:TypeArguments="x:String"Name="strMailFilter"/>
    <Variablex:TypeArguments="sd:DataTable"Name="dtReportAttachments"/>
    </Flowchart.Variables>
    Then you could have writen a quick and dirty extraction function. But as you say, that's not the case.

    With CodeMonkey's input on XAML being valid XML, simply use pugixml – it's a very popular C++ library.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    959
    Adrian's point is that if the input file is simple enough, something like this may do what you want.
    Code:
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
     
    int main() {
        std::ifstream in("input.xaml");
        bool found_start = false;
        std::string line;
     
        // Find start of flowchart variables.
        while (std::getline(in, line)) {
            std::istringstream iss(line);
            std::string token;
            iss >> token;
            if (token == "<Flowchart.Variables>") {
                found_start = true;
                break;
            }
        }
     
        if (!found_start)
            return 1;
     
        // Extract x:String names.
        while (std::getline(in, line)) {
            std::istringstream iss(line);
            std::string token;
            std::getline(iss, token, '=');
            if (token == "</Flowchart.Variables>")
                break;
            if (token == "<Variablex:TypeArguments") {
                std::getline(iss, token, '"');
                std::getline(iss, token, '"');
                if (token == "x:String") {
                    std::getline(iss, token, '"');
                    std::getline(iss, token, '"');
                    std::cout << token << '\n';
                }
            }
        }
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-29-2012, 09:52 AM
  2. how to convert the dialog box to xaml
    By lqq889 in forum Windows Programming
    Replies: 4
    Last Post: 03-28-2010, 11:42 PM
  3. how to convert the dialog box to XAML format?
    By lqq889 in forum Tech Board
    Replies: 3
    Last Post: 03-24-2010, 10:42 PM
  4. new Parser/scanner library
    By lollobrigido in forum Linux Programming
    Replies: 7
    Last Post: 01-29-2009, 08:44 AM
  5. Problem with Apache XML C++ Parser library
    By gandalf_bar in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2004, 09:42 AM

Tags for this Thread