-1

I'm using Python 3 to parse xml document. When I print the data held between the "Host" tags. I get <Element 'Host' at 0x7f2a037fca48>`. How do I get the string data held by the Element?

When I view the document in Google I can drill into the properties of the "Host" element and see:

tagName: "Host"
textContent: "10.10.1.115"

How do get to the "textContent:"?

My code:

import xml.etree.ElementTree as ET
mytree = ET.parse('IPHost-1.xml')
myroot = mytree.getroot()
for x in myroot.findall('IPHostList'):
    Name = x.find('Name').text
    Host = x.find('Host').text
    #HostType = x.find('HostType').text
    #IPAddress = x.find('IPAddress.')
    #print(Name,IPFamily,HostType,IPAddress)

Result 5.5.5.5 IPv4 IP <Element 'Host' at 0x7f2a037fca48>

My XML Code:

<Name>X-RDP-SERVERS</Name>
<Description/>
<HostList>
  <Host>INSE-AA01VRDP014</Host>
  <Host>INSE-AA01VRDP033</Host>
  <Host>INSE-AA01VRDP046</Host>
</HostList>
<IPFamily>IPv4</IPFamily>   </IPHostGroup>   <IPHostGroup>
<Name>I-SYSLOG</Name>
<Description/>
<HostList>
  <Host>A-SPIPLOG01</Host>
  <Host>A-SPIPSEC04</Host>
  <Host>A-NITRO</Host>
</HostList>
<IPFamily>IPv4</IPFamily>   </IPHostGroup>   <IPHostGroup>
<Name>X-DMZ-LOCAL</Name>
<Description/>
<HostList>
  <Host>INSIDE-SPIVDD011</Host>
  <Host>INSIDE-SPIVDD021</Host>
</HostList>
<IPFamily>IPv4</IPFamily>   </IPHostGroup>   <IPHostGroup>
<Name>I-WIFI</Name>
<Description/>
<HostList>
  <Host>10.23.0.0</Host>
</HostList>
<IPFamily>IPv4</IPFamily>   </IPHostGroup>   <IPHostGroup>
<Name>X-SERVERS</Name>
<Description/>
<HostList>
  <Host>10.33.0.0</Host>
  <Host>10.43.0.0</Host>
  <Host>10.1.24.0</Host>
  <Host>10.55.2.0</Host>
</HostList>
<IPFamily>IPv4</IPFamily>   </IPHostGroup> </IPGroupList>
Wiestruth
  • 1
  • 2
  • You need to show your XML too (make a meaningful sample). – Tomalak Jun 11 '20 at 15:06
  • 1
    You'll have to edit your question and post a representative sample of `IPHost-1.xml`. – Jack Fleeting Jun 11 '20 at 15:07
  • Does this answer your question? [selecting attribute values from lxml](https://stackoverflow.com/questions/6126789/selecting-attribute-values-from-lxml) – Joe Jun 11 '20 at 15:30
  • https://stackoverflow.com/questions/42413678/retrieve-attribute-names-and-values-with-python-lxml-and-xpath – Joe Jun 11 '20 at 15:30

1 Answers1

0

Your question isn't clear, but you are probably looking for something like:

for x in myroot.xpath('//Name'):
    print("Name: ",x.text)
    for host in x.xpath('./following-sibling::HostList//Host'):
        print("Host: ",host.text)
    for family in x.xpath('./following-sibling::IPFamily'):
        print("IP Family: ",family.text)

Output (based on your sample xml, properly validated):

Name:  X-RDP-SERVERS
Host:  INSE-AA01VRDP014
Host:  INSE-AA01VRDP033
Host:  INSE-AA01VRDP046
IP Family:  IPv4

etc.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Thanks Jack! Your output is exactly what I'm looking for. I appreciate you taking a stab at answering my question - you are correct - It's not very clear. I have adjusted my question so I hope it's easier to understand. I'm new to Python and teaching mysel so I appreciate the constructive criticism and will work on making my questions clearer and more concise. – Wiestruth Jun 30 '20 at 20:39
  • @Wiestruth Can you edit your question and add the **exact** expected output from your sample xml? – Jack Fleeting Jun 30 '20 at 20:49