这是我试图解析的XML文件的简短版本。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TipsContents xmlns="http://www.avendasys.com/tipsapiDefs/1.0">
<TipsHeader exportTime="Mon May 04 20:05:47 SAST 2020" version="6.8"/>
<Endpoints>
<Endpoint macVendor="SHENZHEN RF-LINK TECHNOLOGY CO.,LTD." macAddress="c46e7b2939cb" status="Known">
<EndpointProfile updatedAt="May 04, 2020 10:02:21 SAST" profiledBy="Policy Manager" addedAt="Mar 04, 2020 17:31:53 SAST" fingerprint="{}" conflict="false" name="Windows" family="Windows" category="Computer" staticIP="false" ipAddress="xxx.xxx.xxx.xxx"/>
<EndpointTags tagName="Username" tagValue="xxxxxxxx"/>
<EndpointTags tagName="Disabled Reason" tagValue="IS_ACTIVE"/>
</Endpoint>
</Endpoints>
<TagDictionaries>
<TagDictionary allowMultiple="false" mandatory="true" defaultValue="false" dataType="Boolean" attributeName="DOMAIN-MACHINES" entityName="Endpoint"/>
<TagDictionary allowMultiple="false" mandatory="true" defaultValue="true" dataType="Boolean" attributeName="IS_ACTIVE" entityName="Endpoint"/>
<TagDictionary allowMultiple="true" mandatory="false" dataType="String" attributeName="Disabled Reason" entityName="Endpoint"/>
<TagDictionary allowMultiple="false" mandatory="false" dataType="String" attributeName="Username" entityName="Endpoint"/>
</TagDictionaries>
</TipsContents>
我运行下面的脚本
import xml.etree.ElementTree as ET
f = open("Endpoint-5.xml", 'r')
tree = ET.parse(f)
root = tree.getroot()
这是我的输出结果
In [8]: root = tree.getroot()
In [9]: root.findall('.')
Out[9]: [<Element '{http://www.avendasys.com/tipsapiDefs/1.0}TipsContents' at 0x10874b410>]
In [10]: root.findall('./TipsHeader')
Out[10]: []
In [11]: root.findall('./TipsContents')
Out[11]: []
In [15]: root.findall('{http://www.avendasys.com/tipsapiDefs/1.0}TipsContents//Endpoints/Endpoint/EndpointProfile')
Out[15]: []
我一直按照这个来做 https:/docs.python.org3libraryxml.etree.elementtree.html#example在其他教程中,但我似乎没有得到一个输出。
我已经尝试了 from lxml import html
我的脚本如下。
tree = html.fromstring(html=f)
updatedAt = tree.xpath("//TipsContents/Endpoints/Endpoint/EndpointProfile/@updatedAt")
name = tree.xpath("//TipsContents/Endpoints/Endpoint/EndpointProfile/@name")
category = tree.xpath("//TipsContents/Endpoints/Endpoint/EndpointProfile/@category")
tagValue = tree.xpath("//TipsContents/Endpoints/Endpoint/EndpointTags[@tagName = 'Username']/@tagValue")
active = tree.xpath("//TipsContents/Endpoints/Endpoint/EndpointTags[@tagName = 'Disabled Reason']/@tagValue")
print("Name:",name)
上面的尝试也没有返回任何结果。
我能够从API中解析XML文档,并成功地使用第二次尝试,但当我从本地文件中这样做时,我没有得到结果。
希望得到任何帮助。
请注意,你的输入XML包含一个默认的命名空间 所以要引用任何元素,你必须指定命名空间。
其中一个方法是定义一个命名空间的字典(捷径 : 全名),在你的情况下。
ns = {'tips': 'http://www.avendasys.com/tipsapiDefs/1.0'}
然后,用 找到所有:
这样做的代码是
for elem in root.findall('./tips:TipsHeader', ns):
print(elem.attrib)
对于你的输入样本,结果是:
{'exportTime': 'Mon May 04 20:05:47 SAST 2020', 'version': '6.8'}
就目前而言 root.findall('.TipsContents') 的时候,即使你像上面一样指定了命名空间,它也会返回一个空列表。
原因是 提示内容 的名称。根 节点,而你试图找到一个同名的元素。下面 inthe XML树,但它不包含这样的元素。
如果你想访问根元素的属性,你可以运行......,但要得到比空字典更多的东西,你必须给根元素添加一些属性(namespace不是属性)。
print(root.attrib)
但要得到比一个空字典更多的东西,你必须给根元素添加一些属性(namespace不是一个属性)。