Extracting data from an XML document that uses namespaces

后端 未结 2 752
独厮守ぢ
独厮守ぢ 2020-12-09 21:14

I have some XML files where I want to use some information from them. I have written a code that reads those files and then looks for some conditions.

The problem is

2条回答
  •  天涯浪人
    2020-12-09 21:35

    Perl has so many excellent XML tools - thanks to all the module developers and libxml2, XML almost seems easy. One of those tools is XML::Dataset - a convenience "scaffolding" module that builds on XML::LibXML and uses a "profile" markup language to grab data from XML sources (NB: The profile mark-up is sensitive to whitespace and line endings).

    e.g.:

    use XML::Dataset;
    use DDP;
    
    my $xml = "Squish.xml" ; 
    open my $fh, "<", $xml or die "aiiieee!";
    my $test_data = do { local $/; <$fh> };
    
    # describe the data using XML::Dataset simplified markup:
    my $data_profile
        = q(
              SquishReport
                test
                  test
                     name = dataset:name);
    
    # parse it with XML::Dataset profile
    my $parsed_data = parse_using_profile($test_data, $data_profile);
    
    # view the element with Data::Printer
    foreach my $element ( $parsed_data->{name}){
         p $element ;
    };
    

    Squish.xml:

    
       
          
          
              
              
                  
                    >  >>  >> start: init (global) - testcase C:\squish\test\tst_start_app]]>
              
           
       
    
    

    Output:

    \ [
        [0] {
            name   "tst_start_app"
        }
    ]
    

提交回复
热议问题