Perl LibXML findvalues(…) concatenates values

前端 未结 2 766
南方客
南方客 2021-01-14 05:27

I am trying to extract node values from an XML file using LibXML. When I call findvalue all of the nodes of the same element type are concatenated. I am totally

2条回答
  •  悲&欢浪女
    2021-01-14 06:13

    Your input data is not easy to process as was indicated by other posters.

    Your code could be as following with provided sample of input data.

    use strict;
    use warnings;
    use feature 'say';
    
    use XML::LibXML;
    
    my $playlistxml = 'playlist.xml';
    
    my $dom = XML::LibXML->load_xml(location => $playlistxml);
    
    foreach my $title ($dom->findnodes('//playlist')) {
        say 'Title: ', $title->findvalue('./title');
        my $tracks = join "\n", map {
            $_->to_literal();
        } $title->findnodes('./tracks/track/@id');
        say $tracks;
        say '';
    }
    

    Sample of input data 'playlist.xml'

    
        
            Yes - Tales From Topographic Oceans
            F28F195257143396 
             
                
                
                
                
            
        
        
            Yes - Yessongs
            Live Album
            405B144877D8B8E4
            
                
                
                
                
                
                
            
         
    
    

    Output

    Title: Yes - Tales From Topographic Oceans
    25912
    25914
    25916
    25918
    
    Title: Yes - Yessongs
    25920
    25922
    25924
    25926
    25928
    25930
    

提交回复
热议问题