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... :)


In reply to XML for Dummies by kevlinux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.