我的data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>10.0</price>
</cd>
<cd country="CHN">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.99</price>
</cd>
<cd country="USA">
<title>Hello</title>
<artist>Say Hello</artist>
<price>0001</price>
</cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)
我的test.pl
#!/usr/bin/perl
# use module
use XML::Simple;
use Data::Dumper;
# create object
$xml = new XML::Simple;
# read XML file
$data = $xml->XMLin("data.xml");
# access XML data
print "$data->{cd}->{country}\n";
print "$data->{cd}->{artist}\n";
print "$data->{cd}->{price}\n";
print "$data->{cd}->{title}\n";
Run Code Online (Sandbox Code Playgroud)
输出:
Not a HASH reference at D:\learning\perl\t1.pl line 16.
Run Code Online (Sandbox Code Playgroud)
评论:我用Google搜索并找到了文章(处理单个xml记录). http://www.go4expert.com/forums/showthread.php?t=812 我测试了文章代码,它在我的笔记本电脑上运行良好.
然后我创建了上面的练习代码以尝试访问多个记录.但失败了.我该如何解决?谢谢.
我是perl的新手,有一个问题,我使用XML :: Simple加载XML,并且我将标记名称作为哈希名称.我想得到以"xsd:schema"名称存储的哈希,但显然$ xsd:schema不起作用.我花了很多年时间用谷歌搜索它,并且无法找到如何做到这一点.
我如何获得该哈希值,以便找出关键值?
编辑:
对不起,我没有很好地解释自己.我想在几个级别的哈希中找出这些键的键和值,但名称xsd:schema导致语法错误:
foreach my $attributes (keys %{ $data{$xsd:schema}{$xsd:element}}){
print "$attributes : ${$data}{$xsd:schema}{$xsd:element}{$attributes}\n";
}
Run Code Online (Sandbox Code Playgroud)
编辑2:我是这样做的.
$schemaData = $data->{'xsd:schema'}->{'xsd:element'}->{'xsd:complexType'}->{'xsd:sequence'}->{'xsd:element'}->{'xsd:complexType'}->{'xsd:sequence'}->{'xsd:element'};
print Dumper($schemaData);
foreach my $fieldName (keys %{ $schemaData}){
$fieldType = $schemaData->{$fieldName}->{'type'};
print "$fieldType\n";
}
Run Code Online (Sandbox Code Playgroud) 我正在使用的JSON数据结构中有布尔值.当调用decode_json将其转换为Perl数据结构并提供给XMLout提供的函数时XML::Simple,它会抛出错误,因为XMLout不知道如何处理JSON::XS::Boolean值.
有没有办法将JSON::XS::Boolean数据结构中的值转换为XML?
my $text = '{"a":"x","b":true}';
my $result = decode_json($text);
my $rec = XMLout( $result, RootName => 'root', SuppressEmpty => 1);
Run Code Online (Sandbox Code Playgroud)
在代码abive中,我得到以下错误 - 无法编码类型的值:JSON :: XS :: Boolean
A print Dumper $result给出:
$result = {
'a' => 'x',
'b' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' )
};
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何解析这个XML中<current></current>前两个(37%和61.8F)的值.我无法弄清楚,因为看起来他们有相同的字段名称并且都在'传感器'...任何帮助将不胜感激...
Perl中的代码:
#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
use LWP::Simple;
$url = 'http://localhostmachine/output/XML/output.xml';
# create object
my $xml = XML::Simple->new();
# read XML file
$data = $xml->XMLin(get($url));
# print output
print Dumper($data);
Run Code Online (Sandbox Code Playgroud)
我正在阅读的XML文件的输出:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="wpman.xsl"?>
<watchport>
<title>
<TxtTitle>Location#1</TxtTitle>
</title>
<sensor>
<type>Combo Sensor (Humidity)</type>
<name>ComboSensor000</name>
<status>OK</status>
<current>37%</current>
<high>42 (10/19/2009 @ 04:05PM)</high>
<low>28 (10/17/2009 @ 11:26AM)</low>
</sensor>
<sensor>
<type>Combo Sensor (Temperature)</type>
<name>ComboSensor000</name>
<status>OK</status>
<current>61.6F</current>
<high>65.8 (10/17/2009 @ 11:26AM)</high>
<low>60.1 (10/19/2009 @ 04:00PM)</low>
</sensor> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用XML :: Simple CPAN模块将我们数据库的输出转换为简单的XML结构.问题是,无论我尝试传递给XML :: Simple的选项,返回的输出都不是我所希望的.
我们尝试输出的数据库表只是一堆带有定义的项目:
CREATE TABLE `items` (
`id` int(8) NOT NULL,
`name` varchar(40) NOT NULL,
`manufacturer` varchar(40) NOT NULL,
`added` datetime NOT NULL,
PRIMARY KEY (`id`)
);
Run Code Online (Sandbox Code Playgroud)
这是我们目前使用的Perl代码:
my $SQL = qq| select * from items |;
my $sth = main::DatabaseQuery($SQL);
my $items;
my $count = 0;
while(my $item = $sth->fetchrow_hashref()) {
$items->[$count] = $item;
$count++;
}
require XML::Simple;
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1);
my $xml = $xs->XMLout($ref);
Run Code Online (Sandbox Code Playgroud)
这将输出以下XML:
<anon id="10000" name="Item …Run Code Online (Sandbox Code Playgroud) 我有以下XML
<?xml version="1.0" encoding="UTF-8"?>
<Objects >
<Item1 elemId="id1" name="view" sort_id="3">
</Item1>
<Item2 elemId="id3" name="view" sort_id="4" >
</Item2>
<Item3 elemId="id5" name="view" sort_id="2">
</Item3>
<Item4 elemId="id9" name="view" sort_id="1">
</Item4>
</Objects>
Run Code Online (Sandbox Code Playgroud)
我想通过属性对该数据进行排序sort_id以获取以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Objects >
<Item4 elemId="id9" name="view" sort_id="1">
</Item4>
<Item3 elemId="id5" name="view" sort_id="2">
</Item3>
<Item1 elemId="id1" name="view" sort_id="3">
</Item1>
<Item2 elemId="id3" name="view" sort_id="4" >
</Item2>
</Objects>
Run Code Online (Sandbox Code Playgroud)
我知道我不能这样做XML::Simple,但我听说我可以接受XML::LibXML。我找不到解决方案。
XML ::简单文档说使用XML文件启动数据结构,使用XMLin('[FILENAME]')...但我有一个内存中的字符串.
我可以直接使用它,还是需要将其保存到文件系统然后加载到文件系统中XMLin?
could not find ParserDetails.ini in /reports/ie/lib/cpan/XML/SAX
at /reports/ie/lib/cpan/XML/SAX.pm line 212
XML::SAX::do_warn('XML::SAX', 'could not find ParserDetails.ini in /reports/ie/lib/cpan/XML/...') called at /reports/ie/lib/cpan/XML/SAX.pm line 62
XML::SAX::load_parsers('XML::SAX') called at /reports/ie/lib/cpan/XML/SAX.pm line 115
XML::SAX::parsers('XML::SAX') called at /reports/ie/lib/cpan/XML/SAX/ParserFactory.pm line 18
XML::SAX::ParserFactory::new('XML::SAX::ParserFactory') called at /reports/ie/lib/cpan/XML/SAX/ParserFactory.pm line 26
XML::SAX::ParserFactory::parser('XML::SAX::ParserFactory', 'Handler', 'XML::Simple=HASH(0x162c6e30)') called at /reports/ie/lib/cpan/XML/Simple.pm line 358
XML::Simple::build_tree('XML::Simple=HASH(0x162c6e30)', 'changelog.xml', 'undef') called at /reports/ie/lib/cpan/XML/Simple.pm line 308
XML::Simple::build_simple_tree('XML::Simple=HASH(0x162c6e30)', 'changelog.xml', 'undef') called at /reports/ie/lib/cpan/XML/Simple.pm line 227
XML::Simple::parse_file('XML::Simple=HASH(0x162c6e30)', 'changelog.xml') called at /reports/ie/lib/cpan/XML/Simple.pm line 195
XML::Simple::XMLin('XML::Simple=HASH(0x162c6e30)', 'changelog.xml') called at perlXML_test.pl line 11
Run Code Online (Sandbox Code Playgroud)