I have a xml file,recipe.xml which i need to parse and i am using "LIBXML" parser.
<?xml version="1.0" encoding="ISO-8859-1" ?> - <recipe name= "Chocolate Chip Bars"> <optional>vanilla,mango,nuts</optional> <step name="Preheat oven to 350 degrees"/> <step name="Melt butter" /> <step name="combine with brown sugar and vanilla in large mixing bow +l"> <dependency use="vanilla"/> </step> <step name="combine with brown sugar and vanilla,nuts in large mixi +ng bowl"> <dependency use="vanilla,nuts,!mango"/> </step> <step name="combine with brown sugar and mango in large mixing bowl +"> <dependency use="mango,!vanilla,!nuts"/> </step> ... </recipe> <recipe name="chocolate cake"> ....
This is the xml which i have taken and it is just an example and it is simliar to the one which i am using.i have an executable by name 'recipe'. so if i run 'recipe -name Chocolate Chip Bars" it will select the recipe name "Chocolate Chip Bars" and its contents. or if i specify 'recipe -name chocolate cake' it will select the recipe name "chocolate cake " and its contents. To select the proper contents, In the command line, i will specify 'recipe -name Chocolate Chip Bars -use "vanilla"',then it should select 'step name=combine with brown sugar and vanilla in large mixing bowl' and if i specify 'recipe -name Chocolate Chip Bars -use "vanilla,nuts",then it should select 'step name=combine with brown sugar and vanilla,nuts in large mixing bowl '.this is where i am facing the problem.what is the best way to select the "dependency" depending on the specification from the command line. my code:
my $parser = XML::LibXML->new(); my $recipeDoc = $parser->parse_file("recipe.xml"); foreach my $recipe ($recipeDoc->getElementsByTagName("recipe")) { $recipeName = $recipe->getAttribute("name"); foreach my $step ($recipe->getElementsByTagName("step")) { foreach my $dep ($step->getElementsByTagName("dependency")) { if($dep->hasAttribute("use") { # this is where i am struck. # here i have to get value from the command line ( and i know ho +w to get the information from command line) and match it with the dep +endency value. (the issue is with matching it with the dependency val +ue after getting the values from command line) } $stepName = $step->getAttribute("name"); } }

In reply to parsing xml file using LIBXML by Madam

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.