2016-08-20 6 views
-2

は、私はこのようなXMLファイルを編集しています:編集何千

<?xml version="1.0"?> 
<rss version="2.0"> 
    <channel> 
    <title>Test Store</title> 
    <link>http://www.example.com</link> 
    <description>An example item from the feed</description> 

    <item> 
     <id>DB_1</id> 
     <title>Diamond Ring</title> 
     <description>The Most Beautiful Diamond Ring</description> 
     <google_product_category>Accessories</google_product_category> 
     <product_type>Rings</product_type> 
     <link>http://www.example.com/rings</link> 
     <image_link>http://www.example.com/ring.jpg</image_link> 
     <condition>new</condition> 
     <availability>in stock</availability> 
     <discount>10.00%</discount> 
     <price>1490.0</price> 
     <brand>ABC</brand> 
     <item_group_id>GROUP_1</item_group_id> 
    </item> 

    <item> 
     <id>DB_2</id> 
     <title>Gold Ring</title> 
     <description>Pretty Gold Ring</description> 
     <google_product_category>Accessories</google_product_category> 
     <product_type>Rings</product_type> 
     <link>http://www.example.com/gold-rings</link> 
     <image_link>http://www.example.com/gold-ring.jpg</image_link> 
     <condition>new</condition> 
     <availability>in stock</availability> 
     <discount>20.00%</discount> 
     <price>500.0</price> 
     <brand>ABC</brand> 
     <item_group_id>GROUP_2</item_group_id> 
    </item> 

</channel> 
</rss> 

あなたはすべての「アイテム」要素では、要素「割引」があることがわかります。私はすべての要素 "割引"を "条件"に変更し、内容として "新しい"を使用したいと思います。後

<discount>10.00%</discount> 

::前

:たとえば、要素の名前を変更するために

<condition>new</condition> 

、私は単純に任意のテキストエディタの機能を置き換える&見つけるを使用することができます。しかし、要素の内容については、数千の異なる値(たとえば10%/ 20%/ 23%など)があるため、それらをすべて「新しい」値に変更する方法を見つけることはできません。

このような編集目的には、どんなアイデアやツールを使用する必要がありますか?

ありがとうございました。

+0

があるさ言語。あなたのプロフィールを見ると、PHPは実用的な選択肢になるでしょう。そのXMLライブラリは他のものと同じくらい狂っているだけで、ネット上には多くの例があり、結果を得るのは比較的簡単です。 –

+1

また、[XSLT](http://www.w3.org/Style/XSL/)は、XMLファイルの操作/変換専用の専用言語と考えることもできます。ほとんどの汎用言語--Java、PHP、Python、VB - は、XSLT 1.0スクリプトを実行するライブラリを持っています。そして、3.0のスクリプトでさえも実行できる[Saxon and Xalan](http://stackoverflow.com/tags/xslt/info)のような専用のエンジンがあります。 – Parfait

+0

これはproblly <20行のコードです。すぐに私の提出を見てください –

答えて

0

実際には、XPathとVTD-XMLとJavaでちょうど10+行...ここにあなたが精通しているプログラミング言語とそのプログラミングにおけるXMLの解析ライブラリを使用することができ、コード

import com.ximpleware.*; 
import java.io.*; 

public class replaceElements2 { 

    public static void main(String[] s) throws VTDException,IOException{ 
     VTDGen vg = new VTDGen(); 
     if (!vg.parseFile("d:\\xml\\rss.xml", false)){ 
      System.out.println("parsing error"); 
      return; 
     } 
     VTDNav vn = vg.getNav(); 
     AutoPilot ap = new AutoPilot(vn); 
     XMLModifier xm = new XMLModifier(vn); 
     ap.selectXPath("/rss/channel/item/discount"); 
     int i=0; 
     byte[] ba="<condition>new</condition>".getBytes(); 
     while((i=ap.evalXPath())!=-1){ 
      xm.remove(); 
      xm.insertAfterElement(ba); 
     } 
     xm.output("d:\\xml\\rss_new.xml"); 
    } 
}