2012-10-31 55 views
1

我需要錯誤日誌:我正在做標記驗證和DTD驗證到XML文件。錯誤日誌列號和行號

XML:

<?xml version="1.0"?> 
<purchaseOrder orderDate="1999-10-20"> 
    <shipTo country="US"> 
     <name>Alice Smith</name> 
     <street>123 Maple Street</street> 
     <city>Mill Valley</city> 
     <state>CA</state> 
     <zip>90952</zip> 
    </shipTo> 
    <billTo country="US"> 
     <name>Robert Smith</name> 
     <street>8 Oak Avenue</street> 
     <city>Old Town</city> 
     <state>PA</state> 
     <zip>95819</zip> 
    </billTo> 
    <comment>Hurry, my lawn is going wild<!/comment> 
    <items> 
     <item partNum="872-AA"> 
      <productName>Lawnmower</productName> 
      <quantity>1</quantity> 
      <USPrice>148.95</USPrice> 
      <comment>Confirm this is electric</comment> 
     </item> 
     <item partNum="926-AA"> 
      <productName>Baby Monitor</productName> 
      <quantity>1</quantity> 
      <USPrice>39.98</USPrice> 
      <shipDate>1999-05-21</shipDate> 
     </item> 
    </items> 
    </purchaseOrder> 

腳本:

use XML::LibXML; 
my $parser = XML::LibXML->new; 
$parser->validation(1); 
$parser->parse_file("purchase.xml"); 

我得到了不正當的日誌,我需要

error.log中: purchase.xml:號線:列號:錯誤文本

回答

0

您未能提供「不當日誌」的輸出。

使用Try::Tiny來捕獲驗證錯誤並將它們解析爲您的首選格式。

use XML::LibXML; 
use Try::Tiny; 
my $parser = XML::LibXML->new; 
$parser->validation(1); 

try { 
    $parser->parse_file("purchase.xml"); 
} catch { 
    warn "XML parsing found errors: $_"; 
    parse_xml_error($_); 
}; 

sub parse_xml_error { 
    my $errors = shift; 
    # ... left as en exercise for the reader 
} 
+0

ķ先生怎麼去嘗試::微型模塊,在PPM和Theoryx5,在那裏我得到這個模塊先生 – user1787633

+0

https://metacpan.org/module/Try::Tiny和 HTTP未找到:/ /code.activestate.com/ppm/Try-Tiny/ – Perleone