in reply to parsing xml file using LIBXML
First your file is not XML. Attributes should be quoted, so <step name= Preheat oven to 350 degrees/> will cause the parser to die.
Even if you fix this, I think your schema (or DTD or whatever) is not adapted. You use attributes where you should be using elements: name should be an element, possibly with an id attribute that would allow you to refer to it. Then you pack things in attributes that you will have to parse further: <dependency use="vanilla,nuts,!mango"/> should probably be written along the lines of: <dependency><use>vanilla</use><use>nuts</use><use type="optional">mango</use></dependency>.
Once you do this, it will be much easier for you to query the document using XPath (read on XPath, it should allow you to get directly the information you want, instead of using the 3 nested loops you have in your code). For multiple use options, I would use something like Getopt::Long and allow multiple use options, from which you can then build the proper XPath query.
It might even make sense to separate further and have the use element be an empty element with an idref to an element that will have the name of the ingredient:
further in the file (or in an other)<dependency><use idref="vanilla"/> <use idref="nut"/><use type="optional" idref="mango"/> </dependency>
<ingredients> <ingredient id="vanilla"><name>vanilla</name><description>...</descr +iption></ingredient> <ingredient id="nut"><name>nuts</name><description>...</description> +</ingredient> <ingredient id="mango"><name>mango</name><description>...</descripti +on></ingredient> </ingredients>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: parsing xml file using LIBXML
by Tanktalus (Canon) on May 12, 2005 at 21:12 UTC | |
by mirod (Canon) on May 13, 2005 at 04:08 UTC |