Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

XML::Generator is really intended for converting existing data structures to XML. If you want to manipulate them a bit before outputting them, I'm going to second the recommendation for XML::Twig, which is easy to use and fairly well documented.

Here's a quickie example for you, though there are better ones at the link above:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig; my $root = "<nodetag />"; my $element; my $firstelem; my $childcnt; $twig = XML::Twig->new( output_encoding => 'utf8', pretty_print => 'record'); # $root is a string containing the starting tag $twig->parse($root); $root = $twig->root; # $root is now the root twig element, and we can modify it $root->set_gi('nodetag_root'); # We can add children to it foreach $childcnt (0 .. 10) { $element = XML::Twig::Elt->new('childtag' => 'child text'); $element->set_att('index',$childcnt); $element->paste('last_child',$root); } # We can modify an arbitrary child $element = $root->first_child('childtag[@index="5"]'); $element->set_text('Number Five, alive!'); # And we can print it, to a filehandle if necessary $twig->print;
It outputs:
<?xml version="1.0" encoding="utf8"?> <nodetag_root> <childtag index="0">child text</childtag> <childtag index="1">child text</childtag> <childtag index="2">child text</childtag> <childtag index="3">child text</childtag> <childtag index="4">child text</childtag> <childtag index="5">Number Five, alive!</childtag> <childtag index="6">child text</childtag> <childtag index="7">child text</childtag> <childtag index="8">child text</childtag> <childtag index="9">child text</childtag> <childtag index="10">child text</childtag> </nodetag_root>

In reply to Re^3: How to create XML tree from non-XML source by AZed
in thread How to create XML tree from non-XML source by H4

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-02-03 14:21 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (26 votes). Check out past polls.