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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The best XML writer module?

Status
Not open for further replies.

ahammad

Programmer
Sep 24, 2007
10
FR
Hello,

I'm trying to write some data to an XML file. What is the best module to write stuff onto an XML file?

I use XML::Simple to read stuff, and it works very well. I'm going to read stuff in using Simple, make some changes, and rewrite it to the same file. I tried XML::Writer but it was rubbish (it writes, but the file is unformatted and hard to read afterwards). Is there a better one out there, or is there a way that XML:Writer can be used to make the XML files at least more readable?

Thanks
 
Are you complaining that it is not "human" readable or that it is not in proper XML format? If the XML format is proper I would say it's doing its job just fine.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
IN the verin of CGI::pretty, there's a command line tool that you could call called XMLpretty

HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi,
I'd use XML::Simple to write the same thing back to the file unless i missed something.. this code is copied from a file so you may need to tweak it per your needs

Code:
#!/usr/bin/perl

use strict;
use XML::Simple;

sub hash2xml {
    my $info = shift;
    my $xs   = XML::Simple->new( ForceArray => 1, KeepRoot => 1, ForceContent => 1, SuppressEmpty => 1, KeyAttr => 'xxx1' );
    my $xml  = $xs->XMLout( $info );
    return $xml;
}

sub xml2hash {
    my $XMLfile = shift;
    my $ref = XML::Simple::XMLin( $XMLfile, ForceArray => 1, KeepRoot => 1, ForceContent => 1, KeyAttr => 'xxx1' );
    return $ref;
}
1;

pass the new xml/hash to hash2xml.. the return of xml2hash is what you want to put back in the file... i think its pretty readable (from memory)


---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Essentially, these files will be read by the computer, but on occasion, a human will have to go in and change stuff manually.

I'll give it a shot with Simple and see what I get...

The other thing, there is an XML::Tidy module. I haven't found a lot of information on it though, but I guess I can use it to indent stuff properly. Has anyone used it before that can provide some suggestions with that?

Thanks
 
BTW 133tcamel, none of the ForceArray, KeepRoot, ForceContent, KeyAttr are recognized.

Also, I do get a hash ref back. How would I use that to print the contents of the hash, or access contents of a specific tag for example?

Thanks
 
valid xml does not need to be indented, it merely adds to the bandwidth weight of the message, as does indented html.

A browser based plug in for firefox may exist to tidy up the indentation of the XML

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top