Search code examples
xmlparsingd

Confused about std.xml - how do I read attributes?


I've just thrown together an XML control file for a command-line tool I'm writing to manage my compilers. My goal is to basically use this XML file to take a statement like compile d -release foo.d made at the shell, process it to form dmd -m64 -O -inline -release -noboundscheck foo.d and execute it. I've reproduced the XML file here for clarity:

<compilers>
<compiler id="d">
    <command>"dmd -m64"</command>
    <modes>
        <mode id="-debug">"-debug -unittest -w -de"</mode>
        <mode id="-release">"-O -inline -release -noboundscheck"</mode>
    </modes>
</compiler>
<compiler id="java">
    <command>"javac"</command>
    <modes>
        <mode id="-debug">"-g -Xlint"</mode>
    </modes>
</compiler>

Now, from my reading of std.xml, I have to do something like this:

import std.xml, std.stdio, std.string, std.file;

void main(string[] args) {
    auto from_command_line = someFunctionThatDealsWithInput(args);
    string s = cast(string)std.file.read("path/to/xml/file.xml");
    check(s);
    auto parsed = DocumentParser(s);
    //deal with the XML somehow to construct a valid command line command
    //send it to the shell
 }

I know how to execute stuff at the shell, but I'm not sure how to make use of the XML document using the std.xml API. In particular, I'm unclear how to get at the content of a node's attributes (but to be honest, the whole thing doesn't make a lot of sense to me).


Solution

  • May I suggest KXML instead (https://github.com/opticron/kxml)? I find it very easy to parse XML with it (especially with builtin XPath requests).