【发布时间】:2012-09-29 14:18:17
【问题描述】:
我在读取 XML 文件时遇到问题。如果您查看下面的 xml,您会看到元素 <fl> ?fl> 而不是 <fl></fl>。我收到错误开始和结束标签不匹配。
如何:
- XML 文件中存在错误,例如
<fl> ?fl>而不是<fl></fl>。如何在读取 xml 时忽略或修复此类错误,而不是使用它进行解析? - 我只想阅读来自
$_->{desc}->[0]->{en}->[0]的内容,而不是来自<de>、<es>、<fl>的内容。
现在我正在读取 XML 文件,例如:
package test::test6382_sunseekingeurope;
use strict;
use warnings;
use test;
use base qw/test/;
use URI::URL;
use XML::Simple;
use Data::Dumper;
use constant TASK_ID => 6382;
use constant CR_TYPE => '6382';
use constant IMAGE_PATH => "/home/testco/public_html/files/";#"images/";
sub new
{
my $class = shift;
my $self = $class->SUPER::new(CR_TYPE, TASK_ID);
bless $self, $class;
my $url = 'http://www.onbip.com/xml/sunseeking9.xml';
my $xml = $self->geturl('url'=>$url);
$self->extract($xml);
}
sub extract{
my $self = shift;
my $xmlfile = shift;
my $xml = new XML::Simple(ForceArray=>1,'KeyAttr' =>'image');
my $data = $xml->XMLin($xmlfile);
foreach(@{$data->{property}}){
my $property = &makeScalar($_->ID->[0]);
my $description = &makeScalar($_->{desc}->[0]->{en}->[0]);
XML:
<property>
<id>226887</id>
<desc>
<en>
Nice house in the center of Alhaurin de la Torre with walking distance to all amenities.
</en>
<es>
Bonita casa mata en Alhaurin de la Torre con vistas a la montana, se puede acceder caminando al centro, colegios, etc.
</es>
<de>
guter zustand, bezaubernde immobilie,
</de>
<fl>
bon n acces par la route, partiellement meubl?a proximit?'?les, partiellement r?v
?fl>
</desc>
</property>
【问题讨论】:
-
请下次使用正确的格式。
-
您展示的代码不读取 XML,它只遍历 Perl 数据结构。我猜你正在使用一个模块来解析 XML,但你所拥有的显然不是 XML...
标签: perl xml-parsing mod-perl