Convert XML to DataSet

  1. 8 months ago

    Marc

    18 Feb 2019 User since 2018
    Edited 8 months ago by Marc

    In a Windows app I created I did let the users supply a XML file/value which it then converted to an in-memory Sqlite database. After that they can use "simple" SQL queries to extract the desired XML content. Such a class would be great for multiple purposes.

    NB: XML is basically the same as a relational database, with related parent/child nodes.

    Or, not convert it to a relational database, but flatten it like this:

    <parent>
       <title>abc</title>
       <child>
          <name>xyz</name>
       </child>
       <child>
          <name>pqr</name>
       </child></parent>

    ... could be converted to a dataset like this:

    parent_title | parent_child_name
    abc | xyz
    abc | pqr
  2. marco

    19 Feb 2019 Administrator User since 2016

    Well, it would work in the simple case you provided but there are a lot more cases where a solution would not so easy. What about attributes for example?

    I really think that a definitive solution would be to add support for a JSON/XML query language.

  3. Marc

    19 Feb 2019 User since 2018
    Edited 8 months ago by Marc

    @marco Well, it would work in the simple case you provided but there are a lot more cases where a solution would not so easy. What about attributes for example?

    A special query language, like LINQ in C#, would be nice, but I always found the XML to SQLite solution working quite well, even with more complex XML structures. An attribute can basically be seen as a child of the element.

    <parent>
       <title>abc</title>
       <child gender="male">
          <name>xyz</name>
       </child>
       <child gender="female">
          <name>pqr</name>
       </child>
    </parent>

    is effectively the same as:

    <parent>
       <title>abc</title>
       <child>
          <gender>male</gender>
          <name>xyz</name>
       </child>
       <child>
          <gender>female</gender>
          <name>pqr</name>
       </child>
    </parent>

    and can be flatten like:

    parent_title | parent_child_gender | parent_child_name
    abc | male | xyz
    abc | female | pqr

    Of course there could be more XML parts like namespaces, but I just used those as prefixes. At least that's the way I handled it.

    Again a query language would be better, but I personally found this a "good enough" solution. Good enough for me ;)

or Sign Up to reply!