Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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
      <library>
        <book>
    ...
                 width="100" height="125" />
        </book>
      </library>
    
  2. or download this
      #!/usr/bin/perl
    
    ...
      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;
    
    ...
      $node->setData('394');
    
      print $doc->toString;
    
  12. or download this
      my($book)  = $doc->findnodes("//book[isbn = '$isbn']");
      my $library = $book->parentNode;
      $library->removeChild($book);
    
  13. or download this
      my $rating = $doc->createElement('rating');
      $rating->appendTextNode('5');
      $book->appendChild($rating);
    
  14. or download this
      $book->appendTextChild('rating', '5');
    
  15. or download this
      my $fragment = $parser->parse_balanced_chunk(
        '<rating>5</rating><price>32.00</price>'
      );
      $book->appendChild($fragment);
    
  16. or download this
      xmllint --format -
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://490846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-07-04 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls