2014-12-29 55 views
0

值大的XML文件是增量開始所以我有一個巨大的XML文件,採用以下格式:改變一個變量在1

<?xml version="1.0" encoding="UTF-8"?> 
<furnidata> 
    <roomitemtypes> 
    <furnitype id="13" classname="shelves_norja"> 
     <revision>49500</revision> 
     <defaultdir>0</defaultdir> 
     <xdim>1</xdim> 
     <ydim>1</ydim> 
     <partcolors> 
     <color>#ffffff</color> 
     <color>#F7EBBC</color> 
     </partcolors> 
     <name>Beige Bookcase</name> 
     <description>For nic naks and books.</description> 
     <adurl/> 
     <offerid>1871</offerid> 
     <buyout>1</buyout> 
     <rentofferid>-1</rentofferid> 
     <rentbuyout>0</rentbuyout> 
     <bc>1</bc> 
     <excludeddynamic>0</excludeddynamic> 
     <customparams/> 
     <specialtype>1</specialtype> 
     <canstandon>0</canstandon> 
     <cansiton>0</cansiton> 
     <canlayon>0</canlayon> 
    </furnitype> 
    <furnitype id="14" classname="shelves_polyfon"> 
     <revision>48082</revision> 
     <defaultdir>0</defaultdir> 
     <xdim>2</xdim> 
     <ydim>1</ydim> 
     <partcolors> 
     <color>0</color> 
     <color>0</color> 
     <color>0</color> 
     </partcolors> 
     <name>Bookcase</name> 
     <description>Make your books look sleek.</description> 
     <adurl/> 
     <offerid>2123</offerid> 
     <buyout>1</buyout> 
     <rentofferid>-1</rentofferid> 
     <rentbuyout>0</rentbuyout> 
     <bc>1</bc> 
     <excludeddynamic>0</excludeddynamic> 
     <customparams/> 
     <specialtype>1</specialtype> 
     <canstandon>0</canstandon> 
     <cansiton>0</cansiton> 
     <canlayon>0</canlayon> 
    </furnitype> 
    </roomitemtypes> 
</furnidata> 

所以那裏有大約1000套不同的「furnitypes」,並保持很簡單,我想(從上到下),每個爐子的'offerid'從1開始,在頂部開始,每個爐子增加1。

因此,第一個爐子的第一個報價爲1,2,第二個爲3,第三個爲第三個,等等。現在每個Furnicpe的offerid都是隨機數字。

即使世界太多的對我來說,手動1.

回答

0

編輯1這真的取決於你有什麼用。例如,這個令人難以置信的醜陋的perl單線程將做到這一點(在Windows中 - 在* nix上,你可能需要單引號)。它從furn.xml中讀取並寫入furn-new.xml

perl -npe "$num = 1 if(!defined($num)); $num++ if (s/furnitype id=\"\d+\"/furnitype id=\"$num\"/g) " <furn.xml> furn-new.xml 

有可能是一個辦法潛行$ NUM ++到表達,但它已經足夠了:)

+0

說實話難看 - 我真的不知道我應該如何使用該行代碼 – Below50

+0

只需安裝perl並將其鍵入命令行。在不知道你有什麼工具可用,以及你最喜歡的編程語言的情況下,人們很難提供幫助。每個人都會有自己喜歡的方式來做到這一點。我的例子對於一次性工作很好,但是對於自動化而言是不安全的,因爲XML格式的細微變化可能會使其無效(它只適用於每行一個標籤)。使用合適的XML解析庫更安全但更復雜。 –

+1

@ Below50我只是補充一點,如果你不習慣爲此編寫一個程序,那麼你最好在http://superuser.com/上詢問,該程序更適用於程序使用。可能有一個工具可以幫助你做到這一點,但這可能是堆棧溢出的主題。 –