6

要素を編集または名前変更してファイルを保存する XML ファイルがあります。それを行う最善の方法は何ですか。XML ファイルを以下に示します。

<breakfast_menu>
<food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
</food>
<food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
</food>
<food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    <calories>900</calories>
</food>
<food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>thick slices made from our homemade sourdough bread</description>
    <calories>600</calories>
</food>
<food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    <calories>950</calories>
</food>
</breakfast_menu>

「説明」を「詳細」に変更するにはどうすればよいですか?

4

3 に答える 3

0

XML が常にこのように単純なままである場合は、正規表現を使用できます。

import re
xml = """
<breakfast_menu>
...
</breakfast_menu>
"""
regex = re.compile('<description>(.*)</description>')
xml = regex.sub(r'<details>\1</details>',xml)
于 2013-04-20T14:34:03.050 に答える
-2

さて、あなたには2つの選択肢があります。xml が小さい場合は、単純な文字列の置換で十分です。xml が非常に大きい場合は、xsl 変換を適用することをお勧めします。

于 2013-04-20T14:21:44.837 に答える