Showing posts with label Perl XML. Show all posts
Showing posts with label Perl XML. Show all posts

Tuesday, 22 April 2014

Perl XML Reading and Writing using LibXML Module

For playing with Nodes, refer to:

http://search.cpan.org/~shlomif/XML-LibXML-2.0110/lib/XML/LibXML/Node.pod 

For playing with elements, refer to:

http://search.cpan.org/~shlomif/XML-LibXML-2.0110/lib/XML/LibXML/Element.pod 

For playing with documents, refer to:

http://search.cpan.org/dist/XML-LibXML/lib/XML/LibXML/Document.pod

A code snippet that use XML::LibXML

http://stackoverflow.com/questions/154762/how-can-i-create-xml-from-perl

Wednesday, 5 March 2014

how-can-i-create-xml-from-perl

Reference: http://stackoverflow.com/questions/154762/how-can-i-create-xml-from-perl 

use strict;
use warnings;
use XML::LibXML;

my $doc = XML::LibXML::Document->new('1.0', 'utf-8');

my $root = $doc->createElement("my-root-element");
$root->setAttribute('some-attr'=> 'some-value');

my %tags = (
    color => 'blue',
    metal => 'steel',
);

for my $name (keys %tags) {
    my $tag = $doc->createElement($name);
    my $value = $tags{$name};
    $tag->appendTextNode($value);
    $root->appendChild($tag);
}

$doc->setDocumentElement($root);
print $doc->toString();

output:

<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
    <color>blue</color>
    <metal>steel</metal>

</my-root-element>
Tweets by @sriramperumalla