There are many ways to cobble this together and do a decent job as GrandFather shows above. Consider doing it right™ however, as GrandFather also alludes with the CPAN link.

Using the right tools is painful at first but forgoing them is false economy and will waste drastically more time and effort. Learning to do it right is also the way to get your resume to the top of the pile. One of the right ways to do XML is XML::LibXML (there are others but not many and writing your own parsing code is definitely not one of them).

use strict; use warnings; use XML::LibXML; my $doc = XML::LibXML::Document->new(); my $root = $doc->createElement("views"); $doc->setDocumentElement($root); while ( <DATA> ) { chomp; my ( $view_id, $view_name, $param_id, $param_name ) = split /\s+/, + $_, 4; # find or create... my ( $view_node ) = $root->findnodes("//view[\@id=$view_id]"); unless ( $view_node ) { $view_node = $doc->createElement("view"); $view_node->setAttribute("name",$view_name); $view_node->setAttribute("id",$view_id); $root->appendChild($view_node); } my $param_node = $doc->createElement("parameter"); $param_node->setAttribute("id", $param_id); $param_name = $doc->createTextNode($param_name); $param_node->appendChild($param_name); $view_node->appendChild($param_node); } print $doc->serialize(1); __DATA__ 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
<?xml version="1.0"?> <views> <view name="all" id="123456"> <parameter id="123">Param 1</parameter> <parameter id="124">Param 2</parameter> </view> <view name="detail" id="123457"> <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>

In reply to Re: Creating simple XML from a DB query by Your Mother
in thread 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.