[英]Element Tree : How to find all the next child elements having same attribute value
I have an XML file with following structure: 我有一个具有以下结构的XML文件:
<?xml version="1.0"?>
<data>
<product>
<Product_Code>code1</Product_Code>
<Size>x</Size>
<Quantity>1<Quantity>
</product>
<product>
<Product_Code>code3</Product_Code>
<Size>c</Size>
<Quantity>5<Quantity>
</product>
<product>
<Product_Code>code2</Product_Code>
<Size>z</Size>
<Quantity>2<Quantity>
</product>
<product>
<Product_Code>code3</Product_Code>
<Size>a</Size>
<Quantity>1<Quantity>
</product>
<product>
<Product_Code>code1</Product_Code>
<Size>y</Size>
<Quantirt>1<Quantity>
</product>
<product>
<Product_Code>code3</Product_Code>
<Size>b</Size>
<Quantity>5<Quantity>
</product>
</data>
There are products in the XML. XML中有产品。 Each product has a code,size and quantoty.
每个产品都有代码,尺寸和数量。 Code can be common.
代码可以是通用的。
I want to select all different size corresponding to each code and quantity corresponding to each size. 我想选择与每个代码相对应的所有不同大小以及与每个大小相对应的数量。 I want to do it on the go.
我想随时随地做。 Eg If iterate through the child elements as and find an element with code = "code1", I'd like to find all other elements in the root withcode = "code1" and their corresponding size and quantity value.
例如,如果遍历子元素as并找到一个代码为“ code1”的元素,我想在根中找到所有其他代码为“ code1”的元素及其相应的大小和数量值。
The actual proble is to generate following XML: 实际的问题是生成以下XML:
<products>
<product>
<Product_Code>code1<Product_Code>
<variants>
<variant>
<size>x</size>
<quantity>1</quantity>
</variant>
<variant>
<size>y</size>
<quantity>1</quantity>
</variant>
</variants>
</product>
<product>
<Product_Code>code2<Product_Code>
<variants>
<variant>
<size>z</size>
<quantity>2</quantity>
</variant>
</variants>
</product>
<product>
<Product_Code>code3<Product_Code>
<variants>
<variant>
<size>a</size>
<quantity>1</quantity>
</variant>
<variant>
<size>b</size>
<quantity>5</quantity>
</variant>
<variant>
<size>c</size>
<quantity>5</quantity>
</variant>
</variants>
</product>
</products>
This is the best I have right now for it being rather late in the evening or morning, depending on how you look at it. 这是我目前最好的,因为它取决于傍晚还是傍晚或早晨。 There will likely need to be some optimization if you are doing this on a large amount of XML, but this should get the job done for now and will work on Python 2.7+.
如果您要对大量XML进行此操作,则可能需要进行一些优化,但这应该可以立即完成,并且可以在Python 2.7+上使用。
This solution uses a dictionary to aggregate the products together and create the variants sub-element. 该解决方案使用字典将产品汇总在一起,并创建变体子元素。 Then it iterates through the dictionary to create the new tree with the product code, and variants as children.
然后,遍历字典以使用产品代码和变体作为子代来创建新树。
import xml.etree.ElementTree as etree
tree = etree.parse('test.xml')
root = tree.getroot()
products = dict()
newroot = etree.Element('products')
for product in root.iterfind("product"):
product_code = product.find('Product_Code')
product.remove(product_code)
product.tag = "variant"
variants = products.setdefault(product_code.text,
etree.Element("variants"))
variants.append(product)
for product in products:
product_element = etree.Element('product')
product_code = etree.Element('Product_Code')
product_code.text = product
product_element.append(product_code)
product_element.append(products[product])
newroot.append(product_element)
result_xml = etree.tostring(newroot)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.