使用文档构建器工厂在Python中解析XML

时间:2009-08-04 19:39:12

标签: python xml parsing

我在STAF和STAX工作。这里python用于编码。我是python的新手。 基本上我的任务是使用Document Factory Parser在python中解析XML文件。

我想解析的XML文件是:

<?xml version="1.0" encoding="utf-8"?>
<operating_system>
  <unix_80sp1>
    <tests type="quick_sanity_test">
      <prerequisitescript>preparequicksanityscript</prerequisitescript>
      <acbuildpath>acbuildpath</acbuildpath>
      <testsuitscript>test quick sanity script</testsuitscript>
      <testdir>quick sanity dir</testdir>
    </tests>
    <machine_name>u80sp1_L004</machine_name>
    <machine_name>u80sp1_L005</machine_name>
    <machine_name>xyz.pxy.dxe.cde</machine_name>
    <vmware id="155.35.3.55">144.35.3.90</vmware>
    <vmware id="155.35.3.56">144.35.3.91</vmware>
  </unix_80sp1>
</operating_system>
  1. 我需要阅读所有标签。
  2. 对于标签machine_name,我需要将它们读入列表 说所有机器名称应该在列表中。 所以在阅读标签后,machname应该是[u80sp1_L004,u80sp1_L005,xyz.pxy.dxe.cde]。

  3. 我还需要所有的vmware标签: 所有属性都应该是vmware_attr = [155.35.3.55,155.35.3.56] 所有vmware值都应该是vmware_value = [144.35.3.90,155.35.3.56]

  4. 除了v​​mware标签和机器名称标签之外,我能够正确读取所有标签: 我使用以下代码:(我是xml和vmware的新手)。需要帮助。

    以下代码需要修改。

    factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(1)
    factory.setIgnoringElementContentWhitespace(0)
    builder = factory.newDocumentBuilder()
    document = builder.parse(xmlFileName)
    
    vmware_value = None
    vmware_attr = None
    machname = None
    
    # Get the text value for the element with tag name "vmware" 
    nodeList = document.getElementsByTagName("vmware") 
    for i in range(nodeList.getLength()): 
    node = nodeList.item(i) 
    if node.getNodeType() == Node.ELEMENT_NODE: 
    children = node.getChildNodes() 
    for j in range(children.getLength()): 
    thisChild = children.item(j) 
    if (thisChild.getNodeType() == Node.TEXT_NODE): 
    vmware_value = thisChild.getNodeValue()
    vmware_attr ==??? what method to use ?
    # Get the text value for the element with tag name "machine_name" 
    nodeList = document.getElementsByTagName("machine_name") 
    for i in range(nodeList.getLength()): 
    node = nodeList.item(i) 
    if node.getNodeType() == Node.ELEMENT_NODE: 
    children = node.getChildNodes() 
    for j in range(children.getLength()): 
    thisChild = children.item(j) 
    if (thisChild.getNodeType() == Node.TEXT_NODE): 
    machname = thisChild.getNodeValue()
    

    还有如何检查标签是否存在。我需要正确编写解析代码。

1 个答案:

答案 0 :(得分:0)

你需要将vmware_value,vmware_attr和machname实例化为列表而不是字符串,所以不要这样:

vmware_value = None
vmware_attr = None
machname = None

这样做:

vmware_value = []
vmware_attr = []
machname = []

然后,要将项目添加到列表,请使用列表中的append方法。 E.g:

factory = DocumentBuilderFactory.newInstance();
factory.setValidating(1)
factory.setIgnoringElementContentWhitespace(0)
builder = factory.newDocumentBuilder()
document = builder.parse(xmlFileName)

vmware_value = []
vmware_attr = []
machname = []

# Get the text value for the element with tag name "vmware"
nodeList = document.getElementsByTagName("vmware")
for i in range(nodeList.getLength()):
    node = nodeList.item(i)
    vmware_attr.append(node.attributes["id"].value)
    if node.getNodeType() == Node.ELEMENT_NODE:
        children = node.getChildNodes()
        for j in range(children.getLength()):
            thisChild = children.item(j)
            if (thisChild.getNodeType() == Node.TEXT_NODE):
                vmware_value.append(thisChild.getNodeValue())

我还编写了一些代码,我认为该代码可以将正确的值附加到vmware_attr和vmware_value。

我不得不假设STAX使用xml.dom语法,所以如果不是这样,你就必须适当地编辑我的建议。