how to use python to parse ossec rules xml
Question: I have a ossec rules XML file, with content like this:

    
    
    <var name="SENSITIVE_DIRECTORY">^/root|^/proc|^/etc|^/$</var>
    <var name="BAD_WORDS_OPS">failure|error|bad |fatal|failed|illegal |denied|refused|unauthorized</var>
    
    <group name="local,ops,syslog,sudo,pam,">
    
      <rule id="101000" level="4">
         <if_sid>5715</if_sid>
         <srcip>!10.83.60.54</srcip>
         <srcip>!10.83.60.55</srcip> 
         <description>Except IPs approved.</description>
       </rule>
    </group>
    

I am trying to use python to parse this xml, but I get this error:

    
    
    xml.etree.ElementTree.ParseError: junk after document element: line 10, column 0
    

Here is the code I am using:

    
    
      from xml.etree import ElementTree
    
      def read_xml(text):
          root = ElementTree.fromstring(text)
          lst_node = root.getiterator("person")
          print lst_node
    
      if __name__ == '__main__':
          read_xml(open("test.xml").read())
    


Answer: wrap your xml with a tag like this

    
    
    import xml.etree.ElementTree as ET
    
    def read_xml(text):
        root = ET.fromstring('<root>'+text+'</root>') # just wrap it with a root tag
        for el in root.iter('srcip'): # I changed the tag to srcip since your sample hasn't got "player"
            print el.text
    
    if __name__ == '__main__':
        read_xml(open("yourfile.xml", "r").read())
    
    !10.83.60.54
    !10.83.60.55
    

