通过指定存在多个儿童的名称来解析XML
我在推断相似的外推时遇到了一些困难,因此将有多个名称不同的孩子的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">
...
<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>
...
我正在尝试以“提议tdv'= 270.095”的值。我已经尝试了Beautifulsoup和ElementTree,但是我只是难以找到语法来指定孩子的名字。 IE因为我无法使用类似的搜索字符串:
Model/Proj/EnergyUse/ProposedTDV
我正在尝试找到更多类似的东西:
Model[Name="Standard"]/Proj/EnergyUse[Name='Efficiency Compliance']/ProposedTDV
或类似的beauftifulsoup(或任何其他XML解析器)。
例如,我尝试过类似的事情
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")
,但我知道那里的语法是错误的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看 [python.docs]:xml。 etree.ElementTree-支持的XPath语法。
我保存了您的 XML 并将其增强了一点(固定错误并添加了一些虚拟节点),以便有一个工作示例。
blob00.xml :
code00.py :
输出:
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:
code00.py:
Output: