Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Problem parsing XML

Moderators: kris

Posted: 03/27/09 07:46:15 Modified: 03/27/09 11:50:28

Hi, I'm a D/Tango newbie. I have to confess I find D/Tango confusing. It seems there is some conflict between D and Tango what versions are compatible etc. Then I find Tango difficult to use, but I do not give in easily. in the end I'll be good at D/Tango. Right now I have a simple problem that have stopped me for a day now. I try to pick up an XML structure:

<serverparms>
    <datawarehouse name='bait'>
        <cmd>start.php</cmd><option>logmode=warn</option>...
    </datawarehouse>
    <datawarehouse name='notwanted'/>
    ...
</serverparms>

I like to search this XML for a <datawarehouse> with name='bait' and pick up all children in an associative array, but I have no success. I tried some promising examples but they all seem to fail: e.g.

auto result = root.query.descendant("country").filter((serverdoc.Node node) {
		return contains!(char) (node.attributes.name("id").value, 'B');});

this example fails to compile with a errmsg something like Visitor do not have a name attribute or similar. ANY HELP HIGHLY APPRECIATED

Author Message

Posted: 03/27/09 18:04:54

Looks like the name() function has an additional required parameter (for namespace purposes), and that example has not been updated. This will probably do what you want:

bool foo (doc.Node node)
{
   auto n = node.attributes.name (null, "name");
   return n && n.value == "bait";
}

foreach (node; doc.query.descendant("datawarehouse").filter(&foo).child)
         Stdout.formatln(">> {}", node.name);

Posted: 03/28/09 07:08:46

Hi Kris, Thanx for the suggestion, however it didn't solve my problem, I still got the error:

myserver.d(48): Error: no property 'name' for type 'Visitor' myserver.d(48): Error: function expected before (), not 1 of type int

The best reference I find in Document.d is this:

Visitor attributes () 
{
   Visitor v = {firstAttr_};
   return v;
}

Unfortunatly this doesn't tell me anything. I try to compile under Linux my command is dmd myserver.d Tango version is 0.99.7. Any suggestions? I'm in the middle of the creek without a paddle :(

Posted: 03/28/09 08:55:50

Hi Lasse,

there were some API changes between Tango 0.99.7 and 0.99.8 (the docs on the site are for the latter), but if it helps, the link below is for the original XPath tutorial and it uses 0.99.7

http://www.code-box.org/article/an-introduction-to-tangos-powerful-xmlpath-api

It would generally be easiest for us to help you if you were to upgrade to the latest version though.

Posted: 03/28/09 13:54:14

Hi, Thanx again, I didn't expect an answer on a Saturday. Of course I upgrade. But that will probably be on Monday, I have invited some friends this evening and they will be late and I be tired tomorrow. But Monday I'll give the new version a try. :)

This is actually my first attempt with D. Many years ago I did some C programming. 6 years ago I tried Java but I got blisters on my fingertips. Since then I used PHP which I like very much. But now I need to create Windows connectors to a data warehouse in Linux and I thought I try D/tango just for the hell of it. As with all language there is only one way to learn practice, practice, practice.

Posted: 03/28/09 16:38:24

we should ask Sean to update his webpage, so that it is equivalent to the tutorial code

Posted: 03/29/09 07:50:57

Hey Guys and many thanks! I upgraded to 0.99.8 and now it compiles and I get the values I want. BUT I have some additional questions: From Kris example:
What is foo? A function?
filter(&foo) What does & in &foo mean?

I would like to have a function which converts XML to associative arrays of arrays, since arrays are simple to understand and use. Do you know of any such function?

Posted: 03/29/09 13:10:27

foo is a method that is called to filter out unwanted results. The & is the operator used to tell the compiler that you pass a pointer to foo , in D speak a delegate (which is basically the same as a function pointer, just with a bit more context).