Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Formatting XML

by njcodewarrior (Pilgrim)
on Apr 01, 2007 at 00:00 UTC ( #607658=perlquestion: print w/replies, xml ) Need Help??
njcodewarrior has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a library of functions to create keyhole markup language (kml) files for displaying positions of oceanographic instrumentation and other assets in the Google Earth client. What I'm looking for is a simple module to format the markup once I've put all of the pieces together, prior to writing it to a file. By formatting I mostly mean proper indentation/spacing. I've looked at some of the XML modules on CPAN, but it's a bit overwhelming. Two which have peaked my interest are XML::Writer and XML::MyXML.

Any other suggestions for a simple way to format markup?

Thanks in advance,
- njcodewarrior

Replies are listed 'Best First'.
Re: Formatting XML
by Herkum (Parson) on Apr 01, 2007 at 03:05 UTC
Re: Formatting XML
by mirod (Canon) on Apr 01, 2007 at 03:39 UTC

    XML::Twig has options to pretty_print the output, so my $formatted_xml= XML::Twig->parse( pretty_print => 'indented', $xml)->sprint should give yoou what you want.

Re: Formatting XML
by Jenda (Abbot) on Apr 01, 2007 at 12:01 UTC

    I find the XML::Writer interface awkward at best. I think it's much easier to build a datastructure and then call either the XML::MyXML::simple_to_xml() or XML::Simple::XMLout() or XML::Rules::ToXML() with the right parameters to emit the XML. You may need to tweak the structure a bit to make sure the XML looks the way it should (eg. to give the module you select a hint what's supposed to be an attribute and what should be a childtag with content). Eg. with XML::Rules the easiest way is to specify the value as a reference to a one element array: ->ToXML( 'root' => { foo => 'hello'}) creates <root foo="hello"/> while ->ToXML( 'root' => { foo => ['hello']}) creates <root><foo>hello</foo></root>. You'll most likely need to do something similar with the other modules.

Re: Formatting XML
by valdez (Monsignor) on Apr 01, 2007 at 17:18 UTC

    I've been using XML::Writer for 3 years now and it works very well for my needs; it cannot convert data structures to XML, but it is perfect if you want to compose XML programmatically; its capability of pretty printing is configurable; a good module in my opinion!

    use strict; use warnings; use IO::String; use XML::Writer; my $struct = { epoch => time(), events => [ { type => 'move', elements => { left => 1, up => 2 } }, { type => 'pen', elements => { down => 1, color => 'red' } }, { type => 'move', elements => { right => 3, up => 1 } }, ], }; print to_xml($struct), "\n"; exit; sub to_xml { my $input = shift; my $buffer; my $io = IO::String->new($buffer); my $xml = XML::Writer->new( OUTPUT => $io, NEWLINES => 0, DATA_MODE => 1, DATA_INDENT => 2, ); $xml->xmlDecl('UTF-8'); $xml->startTag( 'block', 'time' => $input->{epoch} ); foreach my $event (@{ $input->{events} }) { $xml->emptyTag( $event->{type}, %{ $event->{elements} } ); } $xml->endTag('block'); $xml->end(); return $buffer; }
    The code above prints:
    <?xml version="1.0" encoding="UTF-8"?> <block time="1175447784"> <move left="1" up="2" /> <pen color="red" down="1" /> <move right="3" up="1" /> </block>

    Ciao, Valerio

Re: Formatting XML
by Zaxo (Archbishop) on Apr 01, 2007 at 04:45 UTC

    Pod is the simple and efficient way to create docs in perl. Perl comes with programs to convert to other formats.

    After Compline,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://607658]
Approved by McDarren
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-22 02:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.