How can I create XML from Perl?

后端 未结 6 826
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 04:52

I need to create XML in Perl. From what I read, XML::LibXML is great for parsing and using XML that comes from somewhere else. Does anyone have any suggestions for an XML

6条回答
  •  余生分开走
    2020-12-05 05:08

    Just for the record, here's a snippet that uses XML::LibXML.

    #!/usr/bin/env perl
    
    #
    # Create a simple XML document
    #
    
    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();
    

    and this outputs:

    
    
        blue
        steel
    
    

提交回复
热议问题