Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么赢了';t使用PythonElementTree检查元素工作_Python_Xml_Celementtree - Fatal编程技术网

为什么赢了';t使用PythonElementTree检查元素工作

为什么赢了';t使用PythonElementTree检查元素工作,python,xml,celementtree,Python,Xml,Celementtree,我最终决定学习如何用python解析xml。我使用elementtree只是为了得到一个基本的理解。我在CentOS 6.5上使用python 2.7.9。我浏览了以下几页: 在这个论坛上进行了几次搜索,但我遇到了一些问题,我不确定是我的代码还是我试图解析的xml 我需要能够验证某些元素是否在xml中。例如,在下面的xml中,我需要检查元素分析器是否存在,如果存在,则获取属性。然后,如果Analyzer存在,我需要检查location元素并获取文本,然后是name元素并获取文本。我认为下面的

我最终决定学习如何用python解析xml。我使用elementtree只是为了得到一个基本的理解。我在CentOS 6.5上使用python 2.7.9。我浏览了以下几页:

在这个论坛上进行了几次搜索,但我遇到了一些问题,我不确定是我的代码还是我试图解析的xml

我需要能够验证某些元素是否在xml中。例如,在下面的xml中,我需要检查元素分析器是否存在,如果存在,则获取属性。然后,如果Analyzer存在,我需要检查location元素并获取文本,然后是name元素并获取文本。我认为下面的代码将检查元素是否存在:

if element.find('...') is not None
但这会产生不一致的结果,而且它似乎永远也找不到位置或名称元素。例如:

if tree.find('Alert') is not None:
似乎有效,但

if tree.find('location') is not None:

绝对不行。我猜tree.find()函数只适用于顶层

那我怎么做这张支票呢

以下是我的xml:

<?xml version='1.0' encoding='UTF-8'?>
<Report>
  <Alert>
    <Analyzer analyzerid="CS">
      <Node>
        <location>USA</location>
        <name>John Smith</name>
      </Node>
    </Analyzer>
    <AnalyzerTime>2016-06-11T00:30:02+0000</AnalyzerTime>
    <AdditionalData type="integer" meaning="number of alerts in this report">19</AdditionalData>
    <AdditionalData type="string" meaning="report schedule">5 minutes</AdditionalData>
    <AdditionalData type="string" meaning="report type">alerts</AdditionalData>
    <AdditionalData type="date-time" meaning="report start time">2016-06-11T00:25:16+0000</AdditionalData>
  </Alert>
  <Alert>
    <CreateTime>2016-06-11T00:25:16+0000</CreateTime>
    <Source>
      <Node>
        <Address category="ipv4-addr">
          <address>1.5.1.4</address>
        </Address>
      </Node>
    </Source>
    <Target>
      <Service>
        <port>22</port>
        <protocol>TCP</protocol>
      </Service>
    </Target>
    <Classification text="SSH scans, direction:ingress, confidence:80, severity:high">
      <Reference meaning="Scanning" origin="user-specific">
        <name>SSH Attack</name>
        <url> </url>
      </Reference>
    </Classification>
    <Assessment>
      <Action category="block-installed"/>
    </Assessment>
    <AdditionalData type="string" meaning="top level domain owner">PH, Philippines</AdditionalData>
    <AdditionalData type="integer" meaning="alert threshold">0</AdditionalData>
  </Alert>
</Report>

我认为您错过了《深入Python》教程中的一行重要内容(仅从上一行开始):

有一种方法可以搜索后代元素,即子元素、子元素和任何嵌套级别的任何元素

这种方法是在元素名称前面加上
/

tree.find(“someElementName”)
将仅查找名为
someElementName
tree
的直接子元素。如果要在
树中的任何位置搜索名为
someElementName
的元素,请使用
tree.find(“//someElementName”)


/
符号源自XPath。ElementTree模块提供对XPath的有限子集的支持。详细介绍了它所支持的XPath语法部分。

我认为您在深入Python教程中遗漏了一行重要内容(仅从以下内容开始):

有一种方法可以搜索后代元素,即子元素、子元素和任何嵌套级别的任何元素

这种方法是在元素名称前面加上
/

tree.find(“someElementName”)
将仅查找名为
someElementName
tree
的直接子元素。如果要在
树中的任何位置搜索名为
someElementName
的元素,请使用
tree.find(“//someElementName”)

/
符号源自XPath。ElementTree模块提供对XPath的有限子集的支持。详细介绍了它所支持的XPath语法的各个部分

<?xml version='1.0' encoding='UTF-8'?>
<Report>
  <Alert>
    <Analyzer analyzerid="CS">
      <Node>
        <location>USA</location>
        <name>John Smith</name>
      </Node>
    </Analyzer>
    <AnalyzerTime>2016-06-11T00:30:02+0000</AnalyzerTime>
    <AdditionalData type="integer" meaning="number of alerts in this report">19</AdditionalData>
    <AdditionalData type="string" meaning="report schedule">5 minutes</AdditionalData>
    <AdditionalData type="string" meaning="report type">alerts</AdditionalData>
    <AdditionalData type="date-time" meaning="report start time">2016-06-11T00:25:16+0000</AdditionalData>
  </Alert>
  <Alert>
    <CreateTime>2016-06-11T00:25:16+0000</CreateTime>
    <Source>
      <Node>
        <Address category="ipv4-addr">
          <address>1.5.1.4</address>
        </Address>
      </Node>
    </Source>
    <Target>
      <Service>
        <port>22</port>
        <protocol>TCP</protocol>
      </Service>
    </Target>
    <Classification text="SSH scans, direction:ingress, confidence:80, severity:high">
      <Reference meaning="Scanning" origin="user-specific">
        <name>SSH Attack</name>
        <url> </url>
      </Reference>
    </Classification>
    <Assessment>
      <Action category="block-installed"/>
    </Assessment>
    <AdditionalData type="string" meaning="top level domain owner">PH, Philippines</AdditionalData>
    <AdditionalData type="integer" meaning="alert threshold">0</AdditionalData>
  </Alert>
</Report>
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()

for child in root: print child

all_links = tree.findall('.//Analyzer')
try:
        print all_links[0].attrib.get('analyzerid')
        ID = all_links[0].attrib.get('analyzerid')
        all_links2 = tree.findall('.//location')
        print all_links2
        try:
                print all_links[0].text
        except:  print "can't print text location"

        if tree.find('location') is None: print 'lost'
        for kid in tree.iter('location'):
                try:
                        location = kid.text
                        print kid.text
                except: print 'bad'

except IndexError: print'There was no Analyzer element'