2012-08-23 46 views
0

我試圖使用XML::Tidy其他縮進一個XML文件:錯誤 - Perl的

sub reformatXML { 
    # 
    # the only argument to this function is the file name 
    # 
    my $file = $_[ 0 ]; 
    # 
    # create a new XML::Tidy object from $file 
    # 
    my $tidy = XML::Tidy->new('filename' => $file); 
    # 
    # Tidy up the indenting 
    # 
    $tidy->tidy(); 
    # 
    # write out changes back to the file 
    # 
    $tidy->write(); 
    print "$file was reformated.\n"; 
    return 
} 

sub main(){ 
    # 
    # get the current directory in which is the 
    # program running on 
    # 
    #my $current_dir = getcwd; 
    #iterateDir($current_dir); 
    my $file = "/path/to/xml/file/autotest.xml"; 
    reformatXML($file); 
} 

那樣簡單。然而,當我把我的main()功能我得到:

501 Protocol scheme 'd' is not supported d:/UDU/r/tc10.0.0.2012080100_buildA/src/build/kits/tc.dtd 
Handler couldn't resolve external entity at line 2, column 29, byte 73 
error in processing external entity reference at line 2, column 29, byte 73: 
<?xml version="1.0" encoding="iso-8859-1"?> 
<!DOCTYPE kit SYSTEM "tc.dtd"> 
============================^ 
<kit> 
    <contact/> 
at C:/xampp/perl/site/lib/XML/Parser.pm line 187 

我是新來Perl,和我沒有想法爲什麼是錯誤。有人能幫我搞清楚嗎?

XML文件的頭是:

<?xml version="1.0" encoding="iso-8859-1"?> 
<!DOCTYPE kit SYSTEM "tc.dtd"> 
<kit> 
    <contact/> 
    <description>autotest files</description> 
    <history> 
    <hist>06-May-2005     Created</hist> 
    <hist>17-Jun-2005   Add autotest.jar to rtkit</hist> 
    <hist>29-Jun-2005   Remove bits picked up elsewhere</ 
hist> 
    <hist>15-Jul-2005   Added acad_add_note_types.</hist> 
    <hist>20-Sep-2005   Add ai stuff</hist> 
    <hist>31-Oct-2005   DMS BnT fixes</hist> 
    <hist>03-Nov-2005   Pander to kitting's obsession abo 
ut unique filenames</hist> 
    <hist>17-Nov-2005   Add ics schema and junit</hist> 
    <hist>09-Dec-2005   add gdt_autotest</hist> 
    <hist>11-Jan-2006   Merge in P10.0.1.5</hist> 
    <hist>16-Jan-2006  Merge</hist> 
    <hist>26-Jan-2006  Need inclass.plmxml to pass tceng 
_util autotest</hist> 
    <hist>06-Mar-2006  Add qdiff.pl</hist> 
    <hist>09-Mar-2006  Kernel tests need a couple fms cl 
ient files</hist> 
    <hist>10-Mar-2006  Missing dependent library</hist> 
    <hist>19-Jan-2006  Merged from timb_gmo</hist> 
    <hist>17-Jan-2006  GMO Kernel Autotests Implementati 

+1

看起來您的DTD文件中存在錯誤,解析器出現問題。 – scrappedcola

+0

@scrappedcola我知道,這很糟糕,但爲什麼解析器關心DTD文件?它只是應該正確解析它們。 – cybertextron

+1

如果您刪除了您的DTD聲明,它會解析嗎?您的XML可能在其他地方出現錯誤。 – scrappedcola

回答

2

XML ::整潔(或更確切地說,它使用模塊中的一個)似乎期望的文件的絕對路徑是一個有效的網址,而不是。它認爲指定的URL是

d:/UDU/r/tc10.0.0.2012080100_buildA/src/build/kits/tc.dtd 

當它真的

file:///d:/UDU/r/tc10.0.0.2012080100_buildA/src/build/kits/tc.dtd 

我不知道如何解決這個bug。你可以嘗試改變

my $file = "..."; 
reformatXML($file); 

my $file = "..."; 
my $url = URI::file->new($file); 
reformatXML($url); 

這就是眼前的錯誤。除此之外,還有一個相對URL被提供給DTD的問題。這不一定是錯誤的,但它有點奇怪。這意味着tc.dtd必須存在於與autotest.xml相同的目錄中。這是真的嗎?


某些解析器(例如XML :: LibXML)可以選擇避免讀取DTD。這通常是不必要的,因此浪費時間,金錢,CPU和帶寬。尋找這樣的選擇。它可能在由XML :: Tidy繼承的其中一個類的構造函數中。

+0

不應該是d-pipe,例如file:// d |/UDU ...? –

+0

@Len Jaffe,「|」也是可以接受的(儘管你的網址缺少斜線),但是當網址意外地不允許「:」在路徑中時,它是一種黑客攻擊。 「:」是首選。請參閱[文件URI方案](http://en.wikipedia.org/wiki/File_URI_scheme) – ikegami

+0

非常感謝你 –