[ad_1]
First of all, thank you for the seniors who tried to help me.
I’ve a large XML file (below is an example with 2 parents).
Each parent(U7418、U2501) might have subchild、subsubchild…etc like parent U2501 below.
<CircuitRoot Title="projectname" Creator="Lee" Date="2020-08-27">
<Profile Platform="SeriesName" />
<U7418 PartNumber="084.21321" Footprint="PPAK-8P" Value="AONR21321" Description="FET MOS AONR21321 PC DFN 3X3 EP 8P">
<U7418 PartNumber="084.21321" Footprint="PPAK-8P" Ball="4" Pin="G" Page="74" Net="LPS_SW">
<R7403 PartNumber="ZZ.R0402" Info="0R-PAD" Net="GND">
<Q7408 PartNumber="084.03413" Pin="D" />
</R7403>
<R7402 PartNumber="64.20035" Net="LPS_SW_A">
<Q7406 PartNumber="075.00138" Pin="D2" />
</R7402>
<C7401 PartNumber="78.15224" Power="+SDC_IN" />
</U7418>
<U7418 PartNumber="084.21321" Footprint="PPAK" Ball="3" Pin="S#3" Page="74" Power="+SDC_IN" />
</U7418>
<U2501 PartNumber="072.25Q64" Footprint="SOIC8" Value="W25Q64JVSSIQ" Description="IC FLASH">
<U2501 PartNumber="072.25Q64" Footprint="SOIC8" Ball="1" Pin="CS#" Page="25" Net="SPI_CS_ROM_N0">
<R2501 PartNumber="63.47234" Power="3D3V_SPI" />
<R2403 PartNumber="ZZ.R0402" Net="SHD_CS0#">
<U2401 PartNumber="071.01515" Pin="GPIO055/PWM2/SHD_CS0#/BSS_STRAP" />
<R2405 PartNumber="63.R0034" Remark="[email protected]" Net="PCH_RSMRST#">
<R2404 PartNumber="ZZ.R0402" Net="PCH_RSMRST#_R">
<U2401 PartNumber="071.01515" Pin="GPIO114/PS2_CLK0A/EC_SCI#" />
</R2404>
<TP2401 PartNumber="ZZ.PAD14" Pin="1" />
<R1709 PartNumber="63.10234" Net="PM_RSMRST#">
<Q1701 PartNumber="75.27002" Pin="3" />
<CPU1 PartNumber="ZZ.00CPU" Pin="RSMRST#" />
<R6414 PartNumber="64.33035L" Net="M_BIST">
<Q6408 PartNumber="84.T3904" Pin="B" />
<U2401 PartNumber="071.01515" Pin="GPIO246/CMP_VREF0" />
<D6403 PartNumber="83.R2003" Net="GND">
<PG4408 PartNumber="ZZ.CLOSE" Net="PWR_CHG_ACOK">
<PU4401 PartNumber="074.09538" Pin="ACOK" />
<PR4426 PartNumber="64.19635" Power="GND" />
</PG4408>
<U2401 PartNumber="071.01515" Pin="VCI_OVRD_IN/GPIO172" />
</D6403>
<C6405 PartNumber="78.10523" Power="GND" />
</R6414>
</R1709>
</R2405>
</R2403>
<R1832 PartNumber="ZZ.R0402" Net="SPI">
<CPU1 PartNumber="ZZ.00CPU" Pin="SPI0_CS0#" />
</R1832>
</U2501>
<U2501 PartNumber="072.25Q64" Footprint="SOIC8" Power="3D3V_SPI" />
</U2501>
</CircuitRoot>
My goal is check whether has some specify child.tag(ex:R7403、PR4426) under parents(ex: U7418、U2501) ,also check whether Power or Net in specify child.attrib is GND.
So, I want to extract each parent’s(U7418、U2501) child.tag and child.attirb
expect output is
{
U7418:{ R7402:[{PartNumber="64.20035" Net="LPS_SW_A"}], Q7406:[{PartNumber="075.00138" Pin="D2"}]... },
U2501:{ R2403:[{PartNumber="ZZ.R0402" Net="SHD_CS0#"}], PR4426:[{PartNumber="64.19635" Power="GND"}]...},
...
}
but i think this expect output can be changed, since here might have better way to achieve my goal, the key point for me is how to paring this file.
I have tryed
import xml.etree.ElementTree as ET
tree = ET.parse(xml)
parent_map = {}
for p in tree.iter():
for c in p:
if c in parent_map:
parent_map[c.tag].append(c.attrib)
else:
parent_map[c.tag] = [c.attrib]
print(parent_map)
can get all child、subshild…tag and attrib, but i can get the corresponding parent.
[1]: https://i.stack.imgur.com/hpAmA.png
Thank you for your patience in reading to the end,
All help is greatly appreciated!
[ad_2]