Hi

This one has been driving me crazy and i'm sure its pretty simple. I have to create an xml document that looks like this (note xml modules are not an option - i have to hard code the tags)

<views> <view id="123456" name="all"> <parameter id="123">Param 1</parameter> <parameter id="124">Param 2</parameter> </view> <view id="123457" name="detail"> <parameter id="125">Param 3</parameter> <parameter id="126">Param 4</parameter> <parameter id="127">Param 5</parameter> <parameter id="128">Param 6</parameter> </view> </views>
As you can see, views may have many parameters, and my root element <views> may have many views. This data is coming from a database that returns a table like this:
VIEW_ID VIEW_NAME PARAM_ID PARAM_NAME ---------------------------------------------- 123456 all 123 Param 1 123456 all 124 Param 2 123457 detail 125 Param 3 123457 detail 126 Param 4 123457 detail 127 Param 5 123457 detail 128 Param 6

I have tried lots of variations on while loops, keeping track of ids etc, but cannot quite seem to grasp it. Some code is below that i have tried

$xml = "<views>\n"; while ($row = $sth->fetchrow_arrayref){ if (!defined($id{$row->[0]})){ $xml .= $xml . "<view id=\"$row->[0]\" name=\"".$row->[1]."\"> +\n"; $id{$row->[0]} = $row->[0]; if (!defined($param{$row->[3]})){ $xml .= $xml . "<parameter id=\"$row->[3]\">".$row->[4]."< +/parameter>\n"; $id{$row->[3]} = $row->[3]; } }elsif(defined($id{$row->[0]})){ if (!defined($param{$row->[3]})){ $xml .= $xml . "<parameter id=\"$row->[3]\">".$row->[4]."< +/parameter>\n"; } } }
There are a few confusing points for me (where would i put the closing </view> tags for example?)

If anyone has any ideas (if using a module, i will try to get it installed as hard as i can), i would be grateful!

Thanks

Joe

-----

Eschew obfuscation, espouse elucidation!


In reply to Creating simple XML from a DB query by joec_

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.