使用文档构建工厂在Python中解析XML
我正在使用STAF和STAX进行工作,这里用的是Python编程。我对Python还不太熟悉。
我的任务是用Document Factory Parser来解析一个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>
- 我需要读取所有的标签。
对于标签machine_name,我需要把它们读到一个列表里,也就是说,所有的机器名称应该放在一个叫machname的列表中。读取标签后,machname应该是[u80sp1_L004,u80sp1_L005,xyz.pxy.dxe.cde]。
我还需要所有的vmware标签:所有的属性应该放在vmware_attr=[155.35.3.55,155.35.3.56]中,所有的vmware值应该放在vmware_value=[144.35.3.90,155.35.3.56]中。
除了vmware标签和机器名称标签外,我能正确读取所有标签:我使用的代码是:(我对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
你需要把 vmware_value、vmware_attr 和 machname 定义成列表,而不是字符串。所以,不要这样写:
vmware_value = None
vmware_attr = None
machname = None
要这样写:
vmware_value = []
vmware_attr = []
machname = []
然后,要往列表里添加项目,可以使用 append 方法。比如:
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 语法,所以如果不是这样的话,你需要根据实际情况调整我的建议。