Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

xml::Writer query

by GerryD (Initiate)
on Oct 15, 2012 at 08:49 UTC ( [id://999031]=perlquestion: print w/replies, xml ) Need Help??

GerryD has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I need some assistance with the xml writer. I could possbly figure this out myself, but I have a deadline so no time to fiddle. I am writing an xml file with perl as items get updated from the front end by users. my query is that I have to write a xml query where it will use all the inputs given from a database, using foreach statement, we'll not go to the database so here is what I want to achieve. I have a list of devices and ports, I need to create a button based on a filter, the filter would be something like this where node='node1' and port='port1' the xml file gets populated based on filter names and filters can only exist once. so here is an example of the script. ## I wrote this on here, so don't judge on errors, I just need an answer for the main question.
use IO::File; use XML::Writer; my $output = IO::File->new(">wheretowrite.xml"); my $writer = XML::Writer->new(OUTPUT => $output); my @device_list = ( "node1 port1" , "node2 port2" , "node3 port3" ) foreach $node(@device_list) { @get_data = split ( ' ' , $node ); $device_name = @get_data[0]; $port_name = @get_data[1]; $writer->startTag("filter", "name" => "all_events", "sql" => "(where + node ="$device_name and port=$port_name") or (where node="device_nam +e and port=$port_name"), "label" => "just-testing", "view" => "Defaul +t"); $writer->endTag( "filter" ); } # end
As you can see, I need to write the $device_name and $port_name in the single filter, the way it does currently do it (as above) it will do a foreach and try to write the filter xml each time, but as it can only exist once it will end up with a single filter with single device and port. trying to put the foreach inbetween the middle of the xml writer statement, it will return an error. the end result should end in (where node='node1" and port=port1) or (where node='node2" and port=port2) so whether there are 100 nodes and ports or 1000's it must write in that sequence in a single filter. i hope this made sense.

Replies are listed 'Best First'.
Re: xml::Writer query
by McA (Priest) on Oct 15, 2012 at 09:05 UTC

    Hi

    Probably this hint helps:

    my @clauses; foreach $node(@device_list) { my @get_data = split ' ' , $node; push @clauses, '(where node="' . $get_data[0] . "' and port="' . $ +get_data[1] . '")'; } my $filter_string = join ' or ', @clauses; # ...to be continued

    Best regards
    McA

      awesome, thanks alot, that makes sense..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://999031]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found