<xml>
<student name="raj" dept="MCA">
<info>sdfsdf</info>
<course><sub>maths</sub><sub>english</sub></course>
<degree><m>80</m><m>90</m><m>100</m></degree>
</student>
<student name="sathish" dept="BE">
<info>sdfsdf</info>
<course><sub>science</sub><sub>english</sub></course>
<degree><m>70</m><m>90</m><m>100</m></degree>
</student>
<student name="stephen" dept="BTech">
<info>sdfsdf</info>
<course><sub>computer</sub><sub>english</sub></course>
<degree><m>40</m><m>90</m><m>100</m></degree>
</student>
</xml>
代码:
def getXmlElementData(xmlfilename, node, attr, name, element):
collection = getXmlCollectionData(xmlfilename)
nodes = collection.getElementsByTagName(node)
for node in nodes:
if node.hasAttribute(attr) and node.getAttribute(attr) == name:
info = node.getElementsByTagName(element)[0]
elementValue = info.childNodes[0].data
return elementValue
在此示例中,我可以通过传递节点和属性名称来访问信息值。 但是我们如何通过传递xpath来访问sub和m元素 预先感谢