【问题标题】:Perl XML::Smart Out of memory! errorPerl XML::Smart 内存不足!错误
【发布时间】:2014-06-12 22:35:49
【问题描述】:

我有一个小的 Perl 脚本,它下载一个 XML 文件,使用 XML::Smart 解析它,然后通过删除和重新创建一个表将内容复制到 MySQL 数据库中。

这个脚本曾经在 Centos 5 上运行良好,但最近磁盘崩溃了,新驱动器上有 Centos 6。

XML 文件大小为 21.5MB。

我知道它在解析文件时卡住了,因为永远不会删除或创建数据库表。

  my $XML = XML::Smart->new($location.'CategoriesList.xml')
      or die("Unable to parse CategoriesList.xml: $!");;
  $XML = $XML->cut_root();
  $XML = $XML->cut_root();

  $dbh->do("DROP TABLE IF EXISTS ice_categories");
  $dbh->do("CREATE TABLE ice_categories (
      category_id int(11) not null,
      parent_cat_id int(11) not null,
      category_name varchar(100) not null default '',
      category_description varchar(100) not null default '',
      category_image varchar(100) not null default '',
      category_thumb varchar(100) not null default '',
      KEY (category_id), KEY (parent_cat_id))
      CHARACTER SET utf8 COLLATE utf8_unicode_ci;");

  my @Categories = @{$XML->{CategoriesList}{Category}};

  my $c_categories = 0;
  foreach my $category (@Categories) {
    my $cat_name = ucwords($category->{Name}('langid','eq','1')->{Value});
    #print $category->{ID} . " => " . $cat_name . "\n";
    my $cat_desc = $category->{Description}('langid','eq','1')->{Value};
    my $cat_parent = $category->{ParentCategory}{ID};
    $dbh->do("INSERT ice_categories XXXX ");
    $c_categories++;
  }

  print "$c_categories categories imported.\n";
}

我不太擅长 Perl,因此我们将不胜感激。我看过XML::Twig,但我不知道如何在这里使用它。

xml 文件示例。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE ICECAT-interface SYSTEM "http://data.icecat.biz/dtd/ICECAT-   
      interface_response.dtd">
   <ICECAT-interface>
   <Response Date="Sat Apr 26 14:46:53 2014" ID="13219513" Request_ID="1398516412" 
   Status="1">
        <CategoriesList>
        <Category ID="127" LowPic="http://images.icecat.biz/img/low_pic/127-563.jpg"   
        Score="9725" Searchable="0" ThumbPic="http://images.icecat.biz/thumbs/CAT127.jpg" 
         UNCATID="43171520" Visible="0">
         <Description ID="548838" Value="Device or stand where you can rest your mobile or 
        fixed telephone." langid="1"/>
        <Description ID="8310" Value="" langid="2"/>
        <Keywords ID="3274" Value="" langid="1"/>
        <Keywords ID="3275" Value="" langid="2"/>
        <Keywords ID="3276" Value="" langid="3"/>
        <Keywords ID="3277" Value="" langid="4"/>
        <Keywords ID="3278" Value="" langid="5"/>
        <Name ID="255" Value="telephone rests" langid="1"/>
        <Name ID="471173" Value="telefoon steunen" langid="2"/>
        <Name ID="343915" Value="autres téléphones" langid="3"/>

【问题讨论】:

  • 你给我们的时间太少了。您能显示 XML 的内容或指向它的链接吗?
  • 我添加了xml文件的sn-p
  • 这是对使用常量内存(例如:SAX)解析器而不是基于 DOM 的解析器的投票。

标签: mysql xml perl


【解决方案1】:

XML::Twig 的一个不错的功能是它支持twig_handers 来“处理”XML,并且它具有purge 方法,因此您可以将其丢弃以保持内存占用下。

我对@9​​87654324@ 不够熟悉,不能肯定地说100%,但看起来你只是在处理Category 元素。

应该可以工作,但是对于部分 XML 示例(因此无效)我不能完全肯定。

use strict;
use warnings;
use XML::Twig;

sub process_category {
    my ( $twig, $category ) = @_;
    my $cat_name = $category->get_xpath('./Name[@mode="source"]', 0)->text;

    my $cat_name = $category->get_xpath('./Name[@lang="1"]', 0)->text;
    my $cat_desc = $category->get_xpath('./Description[@lang="1"]', 0)->text;

    ### do something with cat_name and cat_desc

    my $cat_parent = $category -> first_child('ParentCategory') -> first_child_text('ID');
    $twig -> purge; #discard data thus far.
}

my $twig =
    XML::Twig->new( twig_handlers => { 'Category' => \&process_category } );
$twig->parsefile('your_xml.xml');

恐怕我不知道“ParentCategory”是从哪里来的,所以我没有解释它。我假设它只是您未显示的 XML 中的一个元素,但它比purge 更复杂,可能没有那么有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2011-08-09
    • 2015-09-20
    相关资源
    最近更新 更多