我对python还是个新手,对XML还是个新手。我正在尝试对XML进行简单的处理,但是不幸的是,经过几天的努力,我已经陷入僵局。
这是我要处理的文件的一个示例(可能是几千次迭代):
<Schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ofdaxml.org/schema" version="01.00.00">
<Envelope SchemaVersion="01.04.00">
<Enterprise>
<Code>www.Google.com</Code>
<Feature>
<Code>Feature1</Code>
<Description>First Feature</Description>
<Option Sequence="1">
<Code>A/Code>
<Features Sequence="1">
<FeatureRef>AA</FeatureRef>
</Features>
</Option>
<Option Sequence="2">
<Code>B</Code>
<Features Sequence="1">
<FeatureRef>BB</FeatureRef>
</Features>
<OptionPrice>
<PriceListRef>1</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
</Option>
</Feature>
</Enterprise>
</Envelope>
我想完成的是:
<Schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ofdaxml.org/schema" version="01.00.00">
<Envelope SchemaVersion="01.04.00">
<Enterprise>
<Code>www.Google.com</Code>
<Feature>
<Code>Feature1</Code>
<Description>First Feature</Description>
<Option Sequence="1">
<Code>A/Code>
<Features Sequence="1">
<FeatureRef>AA</FeatureRef>
</Features>
</Option>
<Option Sequence="2">
<Code>B</Code>
<Features Sequence="1">
<FeatureRef>BB</FeatureRef>
</Features>
<OptionPrice>
<PriceListRef>1</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
<OptionPrice>
<PriceListRef>2</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
<OptionPrice>
<PriceListRef>3</PriceListRef>
<ProductPriceRef>Grade2</ProductPriceRef>
</OptionPrice>
</Option>
</Feature>
</Enterprise>
</Envelope>
我试图突出显示更改,所以不确定是否清楚,但是我向Option Sequence 2 Element添加了两个子元素。本质上,我想复制ProductPriceRef元素的第一个值,并在添加与原始节奏相同的子元素时调用它。
抱歉,如果我没有清楚地解释这一点。以下是我创建循环的失败尝试,该循环将在复制ProductPriceRef(在本例中为“ Grade2”)的原始条目时为Option元素创建适当的子元素:
<Schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.ofdaxml.org/schema" version="01.00.00">
<Envelope SchemaVersion="01.04.00">
<Enterprise>
<Code>www.Google.com</Code>
<Feature>
<Code>Feature1</Code>
<Description>First Feature</Description>
<Option Sequence="1">
<Code>A/Code>
<Features Sequence="1">
<FeatureRef>AA</FeatureRef>
</Features>
</Option>
<Option Sequence="2">
<Code>B</Code>
<Features Sequence="1">
<FeatureRef>BB</FeatureRef>
</Features>
<OptionPrice>
<PriceListRef>1</PriceListRef>
<ProductPriceRef>Grade2<PriceListRef>2</PriceListRef><ProductPriceRef>Grade2</ProductPriceRef></ProductPriceRef>
</OptionPrice>
</Option>
</Feature>
</Enterprise>
</Envelope>
最后,这是我创建的代码(虽然不漂亮,但已尽力而为)。我无法通过append和Insert取得成功,但我的猜测是我没有正确应用它:
import os
import xml.etree.ElementTree as et
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, 'RockwellCond3.XML')
tree=et.parse('RockwellCond3.XML')
root = tree.getroot()
options = root.findall(".//Enterprise//Feature//Option//")
for option in options:
if option.tag == 'OptionPrice':
for optionprice in option:
if optionprice.tag == "PriceListRef" and optionprice.text == "2": # had to include this to avoid recursion
break
elif optionprice.tag == 'ProductPriceRef':
prodpriceref = optionprice.text
newplref = et.SubElement(optionprice,'PriceListRef')
newplref.text = "2"
newprodpriceref = et.SubElement(optionprice,'ProductPriceRef')
newprodpriceref.text = prodpriceref
tree.write('output2.xml')
感谢您为解决我的困境所做的尝试。这就是现在的新手生活。