XML and representations
Perl monks, I have a wee problem I can seem to solve. I don't want to get into should XML::Simple be used, it's not relevant to my question... I don't think. Below is some very simple XML and below that the output after passing it through Dumper. As you can see phone2 is a blank tag (no contents) and it has the oddest representation in the Dumper output. How do I detect that? my $xml= XMLin ($data, ForceArray => 0) For non-blank tags / $xml->{message}->{phone1} works fine. for a blank tag, I get other less interesting data. Suggestions? ADD PHONE 15105551212 YES digits ALERT $VAR1 = { 'type' => 'ADD', 'message' => { 'status' => 'ALERT', 'phone1' => '15105551212', 'custID' => 'digits', 'phone2' => {}, 'wav' => 'YES', 'msgtype' => 'PHONE' } }; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML and representations
On 1/29/10 Fri Jan 29, 2010 1:34 PM, "Bruce Ferrell" scribbled: > Perl monks, > > I have a wee problem I can seem to solve. I don't want to get into > should XML::Simple be used, it's not relevant to my question... I don't > think. Below is some very simple XML and below that the output after > passing it through Dumper. > > As you can see phone2 is a blank tag (no contents) and it has the oddest > representation in the Dumper output. How do I detect that? It's a reference to an empty hash -- not that odd, considering that nested tags in the XML generate nested hashes in the XML tree. I guess an undef value would be better. Use the ref function to see if the value is a reference to a hash. If it is, get the number of keys (scalar keys %{$xml{message}->{phone2}}) and see if there are zero elements. > > my $xml= XMLin ($data, ForceArray => 0) > > For non-blank tags / $xml->{message}->{phone1} works > fine. for a blank tag, I get other less interesting data. > > Suggestions? > > > > > ADD > > PHONE > 15105551212 > > YES > digits > ALERT > > > > > $VAR1 = { > 'type' => 'ADD', >'message' => { > 'status' => 'ALERT', > 'phone1' => '15105551212', > 'custID' => 'digits', > 'phone2' => {}, > 'wav' => 'YES', > 'msgtype' => 'PHONE' > } > }; > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML and representations
Hello Bruce Take a closer look at the docs, and try my $xml = XMLin($data, SuppressEmpty => undef); HTH, Rob Bruce Ferrell wrote: I have a wee problem I can seem to solve. I don't want to get into should XML::Simple be used, it's not relevant to my question... I don't think. Below is some very simple XML and below that the output after passing it through Dumper. As you can see phone2 is a blank tag (no contents) and it has the oddest representation in the Dumper output. How do I detect that? my $xml= XMLin ($data, ForceArray => 0) For non-blank tags / $xml->{message}->{phone1} works fine. for a blank tag, I get other less interesting data. Suggestions? ADD PHONE 15105551212 YES digits ALERT $VAR1 = { 'type' => 'ADD', 'message' => { 'status' => 'ALERT', 'phone1' => '15105551212', 'custID' => 'digits', 'phone2' => {}, 'wav' => 'YES', 'msgtype' => 'PHONE' } }; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML and representations
Thanks Rob, I'm glad you validated the solution I found. I figured out that if the empty tag was suppressed, I could test for the tag being present or not. Bruce On 01/30/2010 04:16 AM, Rob Dixon wrote: > Hello Bruce > > Take a closer look at the docs, and try > > my $xml = XMLin($data, SuppressEmpty => undef); > > HTH, > > Rob > > > Bruce Ferrell wrote: >> >> I have a wee problem I can seem to solve. I don't want to get into >> should XML::Simple be used, it's not relevant to my question... I don't >> think. Below is some very simple XML and below that the output after >> passing it through Dumper. >> >> As you can see phone2 is a blank tag (no contents) and it has the oddest >> representation in the Dumper output. How do I detect that? >> >> my $xml= XMLin ($data, ForceArray => 0) >> >> For non-blank tags / $xml->{message}->{phone1} works >> fine. for a blank tag, I get other less interesting data. >> >> Suggestions? >> >> >> >> >> ADD >> >> PHONE >> 15105551212 >> >> YES >> digits >> ALERT >> >> >> >> >> $VAR1 = { >>'type' => 'ADD', >>'message' => { >> 'status' => 'ALERT', >> 'phone1' => '15105551212', >> 'custID' => 'digits', >> 'phone2' => {}, >> 'wav' => 'YES', >> 'msgtype' => 'PHONE' >>} >> }; > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: XML and representations
You misunderstand my solution Bruce. If you set the options as I described, you will have a hash element that looks like 'phone2' => undef instead of the awkward 'phone2' => {} This would be my preference instead of suppressing the empty element altogether, which leaves no indication at all that the element was present. Another reason not to use XML::Simple ;) HTH, Rob Bruce Ferrell wrote: Thanks Rob, I'm glad you validated the solution I found. I figured out that if the empty tag was suppressed, I could test for the tag being present or not. Bruce On 01/30/2010 04:16 AM, Rob Dixon wrote: Hello Bruce Take a closer look at the docs, and try my $xml = XMLin($data, SuppressEmpty => undef); HTH, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/