Extracting all sub-tags' values in a main tag for XML File no matter the tag name

<GPO>     <Computer>         <ExtensionData>             <Extension xmlns:q1="http://www.microsoft.com/GroupPolicy/Settings/Security"               xsi:type="q1:SecuritySettings">                 <q1:Account>                     <q1:Name>ClearTextPassword</q1:Name>                     <q1:SettingBoolean>false</q1:SettingBoolean>                     <q1:Type>Password</q1:Type>                 </q1:Account>                 <q1:Account>                    <q1:Name>MaximumPasswordAge</q1:Name>                   <q1:SettingNumber>120</q1:SettingNumber>                   <q1:Type>Password</q1:Type>                </q1:Account>              </Extension>        </ExtensionData>     </Computer> </GPO> 

Hi, this is my current XML file saved into C:\XMLFile.xml. How can I change the code to extract all the sub-tags’ values inside each <q1:Account> tag using Python 3.8 instead of just by tag name? I have no experience towards XML parsing and reading with Python before.

This is my code so far:

from xml.dom import minidom  xmlFile = minidom.parse("C:\GPOReportAD.xml")  computer = xmlFile.getElementsByTagName("Computer")[0]  extensionData = computer.getElementsByTagName("ExtensionData")[0]  for i in extensionData.getElementsByTagName("q1:Name"):   for x in extensionData.getElementsByTagName("q1:SettingBoolean"):     print("Result: " + i.firstChild.data + " " + x.firstChild.data)     break  

Expected Output:

ClearTextPassword    false MaxmimumPasswordAge  120 
Add Comment
1 Answer(s)

You are dealing with an xml snippet which uses namespaces, making things a bit complicated. Best way to approach it, I believe, is to use the html (not xml) parser from lxml and use xpath to select the values:

import lxml.html as lh gpo = """[your snippet above]"""  doc = lh.fromstring(gpo) #either: for i in doc.xpath(".//*[local-name()='name']"): #or for i in doc.xpath(".//name", namespaces={'ql':'http://www.microsoft.com/GroupPolicy/Settings/Security'}):     print(i.text,i.xpath('./following-sibling::*[1]/text()')[0]) 

Output:

ClearTextPassword false MaximumPasswordAge 120 
Answered on July 17, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.