我有一些perl代码
my $res = $ua->get( $access->to_url );
if ($res->is_success) {
my $ref = XMLin( $res->content );
my $xml = new XML::Simple;
$data = $xml->XMLin($res->content,ForceArray => 1);
#print $res->content;
for my $purchase ( @{ $data->{PurchaseOrders}->{PurchaseOrder}} )
这点很好。...
但是,当我尝试遍历子元素时,如果只有一个子元素
我收到“不是数组引用”错误
for my $item ( @{$purchase->{LineItems}->{LineItem}} )
{
$itemCode = $item->{ItemCode};
}
XML结构是这样的
PurchaseOrders
PurchaseOrder
LineItems
LineItem
我知道xml简单的问题,我必须使用forceArray,但是我不确定如何在子节点上使用forceArray
我发现这篇关于stackoverflow的文章似乎非常接近我的确切问题,但是我正在努力如何在我的代码中执行它
perl, parsing XML using XML::Simple
$VAR1 = {
'PurchaseOrderID' => '82fa50d6-fd45-4fd2-b42d-035aaaa39a2c',
'LineAmountTypes' => 'Exclusive',
'SentToContact' => 'true',
'AttentionTo' => 'sxxxx',
'Status' => 'AUTHORISED',
'LineItems' => {
'LineItem' => {
'LineAmount' => '57.61',
'Quantity' => '1.0000',
'UnitAmount' => '57.6100',
'LineItemID' => 'e295d55d-68bd',
'Description' => 'xxx',
'ItemCode' => 'xxx',
'TaxAmount' => '11.52',
'AccountCode' => '310',
'TaxType' => 'INPUT2'
}
},
'UpdatedDateUTC' => '2018-10-26T14:19:19.053',
'CurrencyCode' => 'GBP',
'Contact' => {
包括我的打印倾销工具的摘录-请注意,它只是重要部分的摘录,一切都很好,直到达到订单项为止
这也是XML文件
<PurchaseOrder>
<PurchaseOrderID>82fa50</PurchaseOrderID>
<PurchaseOrderNumber>PO-0029</PurchaseOrderNumber>
<Date>2018-10-26T00:00:00</Date>
<DeliveryDate>2018-10-28T00:00:00</DeliveryDate>
<DeliveryAddress>Address/DeliveryAddress>
<AttentionTo>XXX</AttentionTo>
<SentToContact>true</SentToContact>
<Reference>000000078</Reference>
<CurrencyRate>1.000000</CurrencyRate>
<CurrencyCode>GBP</CurrencyCode>
<Contact>
<ContactID>f203ed00-8cd1-4e4d-9b76-f5e7d90a3c19</ContactID>
<ContactStatus>ACTIVE</ContactStatus>
<Name>XXX</Name>
<FirstName>XXXy</FirstName>
<LastName>XXX</LastName>
<Addresses>
<Address>
<AddressType>XXX</AddressType>
<AddressLine1>XXX</AddressLine1>
<AddressLine2>XXX</AddressLine2>
<City>XXX</City>
<Region>XXX</Region>
<PostalCode>XXX</PostalCode>
<Country>GBR</Country>
</Address>
<Address>
<AddressType>XXX</AddressType>
<AddressLine1>Unit 1-3</AddressLine1>
<AddressLine2>XXX</AddressLine2>
<City>XXX</City>
<Region>West Yorkshire</Region>
<PostalCode>POSTCODE</PostalCode>
<Country>GBR</Country>
</Address>
</Addresses>
<UpdatedDateUTC>2018-10-08T17:19:55.083</UpdatedDateUTC>
<DefaultCurrency>GBP</DefaultCurrency>
</Contact>
<BrandingThemeID>2ffe566f-7a88-486a-938c-639d27966197</BrandingThemeID>
<Status>AUTHORISED</Status>
<LineAmountTypes>Exclusive</LineAmountTypes>
<LineItems>
<LineItem>
<ItemCode>xxx</ItemCode>
<Description>des</Description>
<UnitAmount>57.6100</UnitAmount>
<TaxType>INPUT2</TaxType>
<TaxAmount>11.52</TaxAmount>
<LineAmount>57.61</LineAmount>
<AccountCode>310</AccountCode>
<Quantity>1.0000</Quantity>
<LineItemID>e295d55d-68bd-41b0-a0b1-cf1f2d5b7a4f</LineItemID>
</LineItem>
</LineItems>
<SubTotal>57.61</SubTotal>
<TotalTax>11.52</TotalTax>
<Total>69.13</Total>
<UpdatedDateUTC>2018-10-26T14:19:19.053</UpdatedDateUTC>
<HasAttachments>false</HasAttachments>
</PurchaseOrder>
答案 0 :(得分:1)
通过使用XML解析器返回可以理解XML树的对象,可以避免ForceArray问题和混乱的数据结构。如果您了解CSS,Mojo::DOM就是一个不错的选择。
use Mojo::DOM;
my $dom = Mojo::DOM->new->xml(1)->parse($res->decoded_content);
for my $purchase ($dom->find('PurchaseOrders > PurchaseOrder')->each) {
# $purchase is a Mojo::DOM object representing a PurchaseOrder element
for my $item ($purchase->find('LineItems > LineItem')->each) {
# It's unclear if ItemCode is an an attribute or a sub-element; assuming sub-element
my $itemCode = $item->at('ItemCode')->text;
...
}
}
XML::LibXML是另一个可以类似使用的选项,但是使用XPath或DOM而不是CSS来定位元素。
use XML::LibXML qw( );
my $doc = XML::LibXML->load_xml(string => $res->decoded_content);
for my $purchase ($doc->findodes('/PurchaseOrders/PurchaseOrder')) {
# $purchase is a XML::LibXML::Element object representing a PurchaseOrder element
for my $item ($purchase->findnodes('LineItems/LineItem')) {
# It's unclear if ItemCode is an an attribute or a sub-element; assuming sub-element
my $itemCode = $item->findvalue('ItemCode');
...
}
}