Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Stepping up from XML::Simple to XML::LibXML

by grantm (Parson)
on Sep 10, 2005 at 07:20 UTC ( #490846=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download

  1. or download this
                 width="100" height="125" />
  2. or download this
      use warnings;
      my $filename = 'library.xml';
  3. or download this
      use XML::Simple qw(:strict);
      foreach my $book (@{$library->{book}}) {
        print $book->{title}->[0], "\n" 
  4. or download this
      use XML::LibXML;
        my($title) = $book->findnodes('./title');
        print $title->to_literal, "\n" 
  5. or download this
      foreach my $title ($doc->findnodes('/library/book/title')) {
        print $title->to_literal, "\n" 
  6. or download this
      print $_->data . "\n" foreach ($doc->findnodes('//book/title/text()'
  7. or download this
      use XML::Simple qw(:strict);
      my $book = $library->{book}->{$isbn};
      print "$_\n" foreach(@{$book->{author}});
  8. or download this
      use XML::LibXML;
      my $query  = "//book[isbn/text() = '$isbn']/author/text()";
      print $_->data . "\n" foreach ($doc->findnodes($query));
  9. or download this
      my $library  = XMLin($filename, ForceArray => [ 'book' ], KeyAttr =>
    + {});
      foreach my $book (@{$library->{book}}) {
        print $book->{isbn}, "\n" if $book->{pages} > 900;
  10. or download this
      use XML::Simple qw(:strict);
      $book->{pages}->[0] = '394';
      print $xs->XMLout($ref);
  11. or download this
      use XML::LibXML;
      print $doc->toString;
  12. or download this
      my($book)  = $doc->findnodes("//book[isbn = '$isbn']");
      my $library = $book->parentNode;
  13. or download this
      my $rating = $doc->createElement('rating');
  14. or download this
      $book->appendTextChild('rating', '5');
  15. or download this
      my $fragment = $parser->parse_balanced_chunk(
  16. or download this
      xmllint --format -

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://490846]
[choroba]: Good morning!
[Discipulus]: good morning choroba!
[Corion]: Hi choroba, Discipulus! I hope you had a good weekend
erix looks for a red-herring joke but can't find one
Discipulus has worked physically and he rediscovered he is no more wont..
erix instead casts a baleful eye on another mysql q.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-01-23 09:22 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (192 votes). Check out past polls.