Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML::Parser how to reduce pseudo-tag "0"

Status
Not open for further replies.

varakal

IS-IT--Management
Mar 18, 2004
114
US
I have the following XML file in the below format:

Code:
<root>
  <clients id="4">
        <name>temp</name>
        <order>yes</order>
  </clients>
</root>

I am using XML::parser Tree Style and the Dumper gives me:

Code:
$VAR1 = [
          'root',
          [
            {},
            0,
            '
  ',
            'clients',
            [
              {
                'id' => '4'
              },
              0,
              '
        ',
              'name',
              [
                {},
                0,
                'temp'
              ],
              0,
              '
        ',
              'order',
              [
                {},
                0,
                'yes'
              ],
              0,
              '
  '
            ],
            0,
            '
'
          ]
        ];

When you see this, you seee unnecessary '0' tags with new lines as its values. I know it is because of the new lines between the tags. I can write some code to remove things like these but ss there a way that is already built in to get rid of these.
Thanks.
 
Code:
$xml =~ s/>\s+<+?/></g;

The above code does the work, but am wondering if there is something built in XML Parser itself.
 
Unless you need the control of XML::parser or the XML file is rather large, I'd definitely suggest using XML::Simple.
Code:
use Data::Dumper;
use XML::Simple;
my $xs = new XML::Simple;
open my $input, "< test.xml" or die "Cannot Open File\n";
my $data = $xs->XMLin($input);
print Dumper($data);
Which gives the output:
Code:
$VAR1 = {
          'clients' => {
                       'order' => 'yes',
                       'name' => 'temp',
                       'id' => '4'
                     }
        };
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top