2011-06-06 30 views
2

我試圖用XML :: Simple和XML :: Twig來解析XML文件,結果相同。文件中的其他字段工作得很好。嘗試在Perl中解析XML,但長數據字符串被截斷

有問題的文件可以在這裏獲得:

curl -s "http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=130" 

這是與分析器或文件有問題?兩個解析器的輸出結果都是一樣的。字符串中的HTML標籤存儲在XML

輸入字段(XML標籤名爲 '摘要' 中):XML的解析後

<summary type="html">&lt;p&gt;Toxoplasmosis is a disease caused by the parasite &lt;em&gt;Toxoplasma gondii&lt;/em&gt;. More than 60 million people in the U.S. have the parasite. Most of them don't get sick. But the parasite causes serious problems for some people. These include people with weak immune systems and babies whose mothers become infected for the first time during pregnancy. Problems can include damage to the brain, eyes and other organs.&lt;/p&gt;&#xd;^I&#xd;&lt;p&gt;You can get toxoplasmosis from &lt;/p&gt;&#xd;&lt;ul&gt;&#xd;&lt;li&gt;^IWaste from an infected cat&lt;/li&gt;&#xd;&lt;li&gt;^IEating contaminated meat that is raw or not well cooked &lt;/li&gt;&#xd;&lt;li&gt;^IUsing utensils or cutting boards after they've had contact with raw meat &lt;/li&gt;&#xd;&lt;li&gt;^IDrinking infected water &lt;/li&gt;&#xd;&lt;li&gt;^IReceiving an infected organ transplant or blood transfusion&lt;/li&gt;&#xd;&lt;/ul&gt;&#xd;&lt;p&gt;Most people with toxoplasmosis don't need treatment. There are drugs to treat it for pregnant women and people with weak immune systems. &lt;/p&gt;&#xd;&#xd;&lt;p class="NLMattribution"&gt;Centers for Disease Control and Prevention&lt;/p&gt;</summary> 

輸出:

<p>Toxoplasmosis is a disease caused by the parasite <em>Toxoplasma gondii</em>. More than 60 million people in the U.S. have the parasite. Most of them don't get sick. But the parasite causes serious problems for some people. These include people with weak im<p class="NLMattribution">Centers for Disease Control and Prevention</p>to treat it for pregnant women and people with weak immune systems. </p>her organs.</p> 

問題的解決方案: XML文件包含回車「 」,這會導致解析器出現問題。在我下載XML文件後,我刪除了回車符,內容如下:

sed -i 's/&#xd;//g' *.xml 

解析器現在按預期工作。

更新: 回車不影響解析器,只有輸出顯示被截斷和混合起來。刪除它確實解決了我的問題。

+0

如果您知道解決方案,請關閉該問題... – pavel 2011-06-06 15:10:10

+0

實際上 字符不會給解析器帶來問題。當我打印結果時,我懷疑它們會導致問題。特別是如果你在Unix機器上工作。如果將結果輸出到文件中,則應能夠看到整個文本,包括一些^ M字符,這些字符在打印時看起來像文本的一部分。儘管沒有看到你的代碼,但很難說。 – mirod 2011-06-07 06:19:03

+0

是的,這似乎是正確的,mirod。打印的輸出是錯誤的,其中一些部分被移除,其他部分被移除。我已經用這個信息更新了這篇文章。 – BackstreetStruts 2011-06-07 12:48:03

回答

2

我做解析捲曲時,爲管道(使用XML::Twig->new->parse(curl -s "http://..." |)得到一些奇怪的結果:內容出現截斷,從呼叫更改呼叫...

事情看起來更好,如果我解析從創建的文件捲曲的結果,或XML ::嫩枝的本地parseurl方法,那麼結果是恆定的,你想要什麼:

#!/usr/bin/perl 

use strict; 
use warnings; 

use XML::Twig; 

my $twig = XML::Twig->new->parseurl("http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=130"); 
my $summary = $twig->first_elt('summary'); 

print $summary->text, "\n"; 

老實說,我不知道爲什麼會這樣。我會嘗試多看一些,但我懷疑我無能爲力:如果問題出現在XML :: Simple和XML :: Twig中,那麼它可能位於堆棧的較低層,XML ::解析器或expat以及它們與curl的交互。

+1

感謝您的輸入!我嘗試了你的兩個例子,解析(curl ..)和parseurl(..,但第一個沒有工作,第二個也產生了一個截斷(但是不變)的結果。我現在正在研究緩衝限制是否會成爲問題,[http://perl-xml.sourceforge.net/faq/#char_events](http://perl-xml.sourceforge.net/faq/#char_events) 。我也在研究本地的xml文件,通過curl下載,它有完整的文本。 – BackstreetStruts 2011-06-06 12:26:59

+2

你確定你得到的全部內容?在解析url時,嘗試保存完整的XML以查看文本是否全部存在。 – mirod 2011-06-06 12:43:23

+0

是的,來自本地XML通過XML :: Twig-> new-> parsefile的輸出和parseurl的輸出是相同的。 – BackstreetStruts 2011-06-06 12:49:55