Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: How to create XML tree from non-XML source

by dHarry (Abbot)
on Sep 08, 2008 at 15:17 UTC ( [id://709791]=note: print w/replies, xml ) Need Help??

in reply to How to create XML tree from non-XML source

There are many Perl modules available for generating/creating XML: Any::Renderer::XML (generates "element only" XML), XML::Generator or XML::Writer to name a few. It depends a bit on what you really need in terms of XML features and how far you wanna push it.

For xpath you can use XML::XPath.

I am using XML::Twig a lot lately. It is turning into a one-solution-for-all-xml-problems for me:-)

Hope this helps

  • Comment on Re: How to create XML tree from non-XML source

Replies are listed 'Best First'.
Re^2: How to create XML tree from non-XML source
by H4 (Acolyte) on Sep 08, 2008 at 17:52 UTC
    Thank you for your suggestions. XML::Generator looks good. But how can I manipulate a tree created with XML::Generator?

      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>
        I wasn't aware I could use XML::Twig to create trees! Having found the (poorly documented) package XML::Twig::XPath, I think I can now do all I want to do. Thank you everybody for helping me. The discussion about data formats and representations is very interesting, please keep on - I'll be watching and silently learning from it.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://709791]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-21 21:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found