Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185681
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T04:20:21+00:00 2022-06-10T04:20:21+00:00

Parsing all child & subchild element by parent from xml in python

  • 0

[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]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 399 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 371 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 367 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.