kevlinux has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to wrap my ahead around an XML config file I'm trying to implement, more for learning than anything else. I find that a lot of the scripts I write (Perl, Bash, etc.) all start with the same 8-10 variables being defined at the top. Change one directory/IP/user, and you have to hunt for all the scripts which will now break. So, I thought what if I made an XML file and coded the scripts to pull from the file. If something changes, just change the XML! Maybe I'm building a bird house with a chainsaw, but I wanted to try.
Then I thought "well, I can add xxx and yyy to it too!" Maybe that was a mistake. ;)
XML config example:<Config> <Variableconfig> <Variable> <Name>NAME</Name> <Value>somevalue</Value> </Variable> ... </variableconfig> <ServerConfig> <Server> <Name>blue</Name> <IP>xxx.xxx.xxx.xx</IP> <IP>xxx.xxx.xxx.xx</IP> <FQDN>blue.some.domain</FQDN> </Server> ... </ServerConfig> </Config>
So, I got the XML file done and slurped it in with XML::Simple using forcearray and Dumped it with data dumper. What I have are hashes of arrays of hashes etc.,etc. I can access elements with $xml->{yadda}[0]->{yadda}[0], but that seemed awfully painful.
So, I thought I'd give XML::LibXML a shot. This seemed a little more straight forward, although there's no visible data structure created that I could tell (I'm a visual type). I could get the file in, but couldn't do something like "find the element with the name 'Name'". I was trying various functions of LibXML (getAttributes, etc.) trying to walk the tree with no luck.
I guess to end this book, is there someone who could give me an example of "find the elements labeled 'Server' and pull out all FQDNs" using XML::LibXML or XML::Simple for that matter? Or suggest a better/different way of doing all this?
Sorry for the long read... :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML for Dummies
by tilly (Archbishop) on Jul 12, 2008 at 04:13 UTC | |
by massa (Hermit) on Jul 12, 2008 at 09:21 UTC | |
by tilly (Archbishop) on Jul 12, 2008 at 14:09 UTC | |
by massa (Hermit) on Jul 12, 2008 at 15:14 UTC | |
by tilly (Archbishop) on Jul 12, 2008 at 15:39 UTC | |
|
Re: XML for Dummies
by Anonymous Monk on Jul 12, 2008 at 04:00 UTC | |
|
Re: XML for Dummies
by Jenda (Abbot) on Jul 12, 2008 at 11:23 UTC | |
|
Re: XML for Dummies
by kevlinux (Initiate) on Jul 12, 2008 at 18:12 UTC |