RE: Help with accessing an unknown set of data generated by XML::Simple
Dale Puckett <[email protected]> Thu, 23 Feb 2012 23:10:49 +0000
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <56B4BC5D7572F448831D1681DB344A9B14A4E2C3@BW-Exchange01.brainworks.local> |
Thanks Francisco and all who have made suggestions!
Thanks to your suggestion I was able to make some progress between support calls and administrative tasks.
I have attached a much shorter sample file and a short output file generated by an extractor created from it. The db:adID and similar coding I used to tell the program what data to put in the content of the output.
Essentially I have written a program that writes a program. I'm trying to improve it, especially in the writing phase. The present code generates a list of ads that need to be extracted for the day with a database query that returns most of the desired output information. The extractor then loops through the database output and processes the information for each ad. The extractor program is an extractor specific to a specific newspaper site and a vendor. Some newspapers run up to 10 different XML extractions for different vendors. I have automated the task.
To improve it I was thinking I could use XMLin to create the output, then plug in the data from the database and output it directly with XMLout. Or, and I'm leaning toward this one ... write out the data with XML::Write. I can then control the CDATA, Attributes another elements handily.
There are several problems with the XMLin --> XMLout approach:
1. I need to figure out how to be able to put out the XML in the same order contained in the Sample file as that is what the vendors expect.
2. I having trouble wrapping my head around a way to access each node and get its value when I do not know the name of the node. The example Francisco provided does a great job pulling out a list of known nodes that are presented in the @WANTED array, but I haven't figured out how to generalize it so that I get all of the nodes, their names and values when they exist.
Here's what I have done so far. It does print out the key and the value for each value but I still need to get the node key and node values into an array or something I can read outside the subroutine so that I can populate the value with the data from our database and then write out the entire node with XML::Writer.
-----
#!/usr/bin/perl
use common::sense;
use feature 'say';
use XML::Simple;
use Data::Dumper qw(Dumper);
my @WANTED=qw(ADS AD ADID ACCTID STARTDATE);
my $xml = XMLin( 'files/test.xml', KeepRoot => 1, );
my %values=map{$_ => &KeysWithValues($_,$xml)} @WANTED;
foreach (keys %values) {
say "$_, $values{$_}";
my $nodekey = $_; my $nodevalue = $values{$_}; say "KEY: $nodekey"; say "VALUE: $nodevalue";
# Need to get these into an array
}
# Now I need try to populate the value of each key with the output from our
# our database and our asset files.
# Must have an array that contains each $nodekey and $nodevalue accessible here
# then I can write out the data with name and value using XML::writer
# My attempt to get all the keys and values in their own $ variables
sub KeysWithValues {
my ( $key, $hashref ) = @_;
foreach my $k ( keys %{$hashref} ) {
if ( ref $hashref->{$k} ~~ q{HASH} )
{
my $val=&KeysWithValues( $key, $hashref->{$k} );
# say "I, $key, am a normal node because ... I am a HASH and have a value, $val";
return $val if $val;
}
else
{
return $hashref->{$k} if $k ~~ $key;
}
}
}
-----
All suggestions welcome. I'm really having a lot of trouble getting the concept of these Hashes wrapped into my head.
Thanks,
Dale
-----
Dale L. Puckett, Director of Operations
Midwest Division, Brainworks Software Development Corporation
7570 West 21st Street North, Building 1010C, Wichita, KS 67205
[email protected], 631-963-5555
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
test.xml
(application/xml, 164 B)
<?xml version="1.0" encoding="UTF-8"?>
<ads>
<ad>
<ADID>db:adID</ADID>
<ACCTID>db:accountID</ACCTID>
<STARTDATE>db:startDate</STARTDATE>
</ad>
</ads>
BW_GoddardAds_20080921.xml
(application/xml, 510 B)
<?xml version="1.0" encoding="Windows-1252" standalone="no"?> <ads> <ad> <ADID>1004213</ADID> <ACCTID>20000972</ACCTID> <STARTDATE>2008-07-04T00:00:00</STARTDATE> </ad> <ad> <ADID>1004214</ADID> <ACCTID>20000972</ACCTID> <STARTDATE>2008-07-05T00:00:00</STARTDATE> </ad> <ad> <ADID>1004271</ADID> <ACCTID>20000972</ACCTID> <STARTDATE>2008-07-04T00:00:00</STARTDATE> </ad> <ad> <ADID>1004272</ADID> <ACCTID>20000972</ACCTID> <STARTDATE>2008-07-04T00:00:00</STARTDATE> </ad> </ads>