Home > Software design >  Parsing XML by specifying name of child where multiple exist
Parsing XML by specifying name of child where multiple exist

Time:04-20

I'm having some trouble extrapolating similar SO threads to a larger XML where there are multiple children with different names. For example, here is a subset of a file I'm working with:

<?xml version="1.0" encoding="UTF-8"?>
<SDDXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <RulesetFilename file="T24N_2022.bin"/>
  <Model Name="Proposed">
    ...
  <Model Name="Standard">
    <Proj>
      <Name>project0001</Name>
      <DevMode>1</DevMode>
      <BldgEngyModelVersion>16</BldgEngyModelVersion>
      <AnalysisVersion>220070</AnalysisVersion>
      <CreateDate>1650049043</CreateDate>
      <EnergyUse>
        ..
      <EnergyUse>
        <Name>Efficiency Compliance</Name>
        <EnduseName>Efficiency Compliance</EnduseName>
        <ProposedTDV index="0">270.095</ProposedTDV>
        <StandardTDV index="0">99.089</StandardTDV>
...

And I'm trying to the the value of 'ProposedTDV' = 270.095. I've tried BeautifulSoup and ElementTree, but I'm just having trouble finding the syntax to specify the name of a child. Ie since I can't use a search string like:

Model/Proj/EnergyUse/ProposedTDV

I'm trying to find something more like:

Model[Name="Standard"]/Proj/EnergyUse[Name='Efficiency Compliance']/ProposedTDV

or similar that I could use with BeauftifulSoup (or any other XML parser).

For example, I've tried things like

from bs4 import BeautifulSoup
result = open(--xml_file_path--,'r')
contents = result.read()
soup = BeautifulSoup(contents,'xml')
test = soup.Model[Name="Proposed"].Proj.EnergyUse[Name='Efficiency Compliance'].findAll("ProposedTDV")

But I know that the syntax there is wrong.

CodePudding user response:

Take a look at [Python.Docs]: xml.etree.ElementTree - Supported XPath syntax.

I saved your XML and enhanced it a bit (fixed errors and added some dummy nodes), in order to have a working example.

blob00.xml:

<?xml version="1.0" encoding="UTF-8"?>
<SDDXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <RulesetFilename file="T24N_2022.bin"/>
  <Model Name="Proposed">
    <Proj>
      <!-- Other nodes -->
      <EnergyUse>
        <Name>Efficiency Compliance</Name>
        <EnduseName>Efficiency Compliance</EnduseName>
        <ProposedTDV index="0">1.618</ProposedTDV>
        <StandardTDV index="0">9.809</StandardTDV>
      </EnergyUse>
    </Proj>
  </Model>
  <!-- Other nodes -->
  <Model Name="Standard">
    <Proj>
      <Name>project0001</Name>
      <DevMode>1</DevMode>
      <BldgEngyModelVersion>16</BldgEngyModelVersion>
      <AnalysisVersion>220070</AnalysisVersion>
      <CreateDate>1650049043</CreateDate>
      <EnergyUse/>
      <!-- Other nodes -->
      <EnergyUse>
        <!-- Only this one should be selected! -->>
        <Name>Efficiency Compliance</Name>
        <EnduseName>Efficiency Compliance</EnduseName>
        <ProposedTDV index="0">270.095</ProposedTDV>
        <StandardTDV index="0">99.089</StandardTDV>
      </EnergyUse>
      <EnergyUse>
        <Name>Some name that SHOULD NOT MATCH</Name>
        <EnduseName>Efficiency Compliance</EnduseName>
        <ProposedTDV index="0">3.141593</ProposedTDV>
        <StandardTDV index="0">2.718282</StandardTDV>
      </EnergyUse>
    </Proj>
  </Model>
</SDDXML>

code00.py:

#!/usr/bin/env python

from xml.etree import ElementTree as ET
import sys


def main(*argv):
    doc = ET.parse("./blob00.xml")
    root = doc.getroot()
    search_xpath = "./Model[@Name='Standard']/Proj/EnergyUse[Name='Efficiency Compliance']/ProposedTDV"

    # Below are different (less restrictive) filters. Decomment each and see the differences
    #search_xpath = "./Model/Proj/EnergyUse[Name='Efficiency Compliance']/ProposedTDV"
    #search_xpath = "./Model[@Name='Standard']/Proj/EnergyUse/ProposedTDV"
    #search_xpath = "./Model/Proj/EnergyUse/ProposedTDV"

    for proposedtdv_node in root.iterfind(search_xpath):
        print("{:}\nText: {:s}".format(proposedtdv_node, proposedtdv_node.text))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q071929246]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

<Element 'ProposedTDV' at 0x00000188CBBEE900>
Text: 270.095

Done.
  • Related