简体   繁体   English

使用 xml 并导出节点名称

[英]Working with xml and exporting names of nodes

I wrote this code below.我在下面写了这段代码。 In my XML file I have nodes:在我的 XML 文件中,我有节点:

Assembly_1, Detail_1, Detail_2, Assembly_2, Detail_3

What I am trying to do is to get the name of the assembly for each detail (Detail_1 and 2 would be in Assembly_1, etc.)我想要做的是获取每个细节的程序集名称(Detail_1 和 2 将在 Assembly_1 等中)

I have a lot of details... more than 200. So this code (function) works good but it takes a lot of time because the XML file is loaded each time.我有很多细节......超过200个。所以这个代码(函数)运行良好,但需要很多时间,因为每次加载XML文件。

How can I make it run faster?我怎样才能让它运行得更快?

def CorrectAssembly(detail):

    from xml.dom import minidom

    xml_path = r"C:\Users\vblagoje\test_python_s2k\Load_Independent_Results\HSB53111-01-D_2008_v2-Final-Test-Cases_All_1.1.xml"
    mydoc=minidom.parse(xml_path)
    root = mydoc.getElementsByTagName("FEST2000")
    assembly=""

    for node in root:
        for childNodes in node.childNodes:
            if childNodes.nodeType == childNodes.TEXT_NODE: continue

            if childNodes.nodeName == "ASSEMBLY":
                assembly = childNodes.getAttribute("NAME")
            if childNodes.nodeName == "DETAIL":
                if detail == childNodes.getAttribute("NAME"):
                    break

    return assembly

One solution is, to simply read the XML-file once before looking up all the details.一种解决方案是,在查找所有详细信息之前简单地读取一次 XML 文件。
Something along this:这方面的事情:

from xml.dom import minidom


def CorrectAssembly(detail, root):

    assembly=""

    for node in root:
        for childNodes in node.childNodes:
            if childNodes.nodeType == childNodes.TEXT_NODE: continue

            if childNodes.nodeName == "ASSEMBLY":
                assembly = childNodes.getAttribute("NAME")
            if childNodes.nodeName == "DETAIL":
                if detail == childNodes.getAttribute("NAME"):
                    break

    return assembly


xml_path = r"C:\Users\vblagoje\test_python_s2k\Load_Independent_Results\HSB53111-01-D_2008_v2-Final-Test-Cases_All_1.1.xml"
mydoc=minidom.parse(xml_path)
root = mydoc.getElementsByTagName("FEST2000")

aDetail = "myDetail"
assembly = CorrectAssembly(aDetail, root)
anotherDetail = "myDetail2"
assembly = CorrectAssembly(anotherDetail , root)
# an so on

You still go through (part of) the loaded XML every time you call the function though.每次调用该函数时,您仍然会浏览(部分)加载的 XML。 Maybe it is beneficial to create a dictionary mapping the assembly to details and then to simply look them up when you need it:创建一个将程序集映射到详细信息的字典然后在需要时简单地查找它们可能是有益的:

from xml.dom import minidom

# read the xml
xml_path = r"C:\Users\vblagoje\test_python_s2k\Load_Independent_Results\HSB53111-01-D_2008_v2-Final-Test-Cases_All_1.1.xml"
mydoc=minidom.parse(xml_path)
root = mydoc.getElementsByTagName("FEST2000")

detail_assembly_map = {}

# fill the dictionary
for node in root:
    for childNodes in node.childNodes:
        if childNodes.nodeType == childNodes.TEXT_NODE: continue
        if childNodes.nodeName == "ASSEMBLY":
            assembly = childNodes.getAttribute("NAME")
        if childNodes.nodeName == "DETAIL":
            detail_assembly_map[childNodes.getAttribute("NAME")] = assembly

# use it
aDetail = "myDetail"
assembly = detail_assembly_map[aDetail]

From your post it is not really clear how the structure of the XML is, but in case the details are children of the assemblies , then the mapping could be done differently by iterating first through the assembly-knots and therein through its detail-children .从您的帖子来看,XML 的结构并不清楚,但如果detailsassembly 的子级,那么映射可以通过首先迭代assembly-knots并在其中迭代其detail-children 来完成 Then you would not rely on a proper ordering of the elements.那么你就不会依赖于元素的正确排序。

This post could be helpful too, depending on the structure of your XML-tree. 这篇文章也可能有所帮助,具体取决于您的 XML 树的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM