Home > Blockchain >  Parse xml for text of every specific tag not working
Parse xml for text of every specific tag not working

Time:12-17

I am trying to gather every element <sequence-number> text into a list. Here is my code

#!/usr/bin/env python

from lxml import etree 

response = '''
<rpc-reply xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:0d07cdf5-c8e5-45d9-89d1-92467ffd7fe4">
 <data>
  <ipv4-acl-and-prefix-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ipv4-acl-cfg">
   <accesses>
    <access>
     <access-list-name>TESTTEST</access-list-name>
     <access-list-entries>
      <access-list-entry>
       <sequence-number>1</sequence-number>
       <remark>TEST</remark>
       <sequence-str>1</sequence-str>
      </access-list-entry>
      <access-list-entry>
       <sequence-number>10</sequence-number>
       <grant>permit</grant>
       <source-network>
        <source-address>10.10.5.0</source-address>
        <source-wild-card-bits>0.0.0.255</source-wild-card-bits>
       </source-network>
       <next-hop>
        <next-hop-type>regular-next-hop</next-hop-type>
        <next-hop-1>
         <next-hop>10.10.5.2</next-hop>
         <vrf-name>SANE</vrf-name>
        </next-hop-1>
       </next-hop>
       <sequence-str>10</sequence-str>
      </access-list-entry>
      <access-list-entry>
       <sequence-number>20</sequence-number>
       <grant>permit</grant>
       <source-network>
        <source-address>10.10.6.0</source-address>
        <source-wild-card-bits>0.0.0.255</source-wild-card-bits>
       </source-network>
       <next-hop>
        <next-hop-type>regular-next-hop</next-hop-type>
        <next-hop-1>
         <next-hop>10.10.6.2</next-hop>
         <vrf-name>VRFNAME</vrf-name>
        </next-hop-1>
       </next-hop>
       <sequence-str>20</sequence-str>
      </access-list-entry>
     </access-list-entries>
    </access>
   </accesses>
  </ipv4-acl-and-prefix-list>
 </data>
</rpc-reply>
'''
q = etree.fromstring(response)
print(q.findall('.//sequence-number'))

But get nothing for output. I have tried the following statements too with no luck:

print(q.findall('./sequence-number/'))
print(q.findall('sequence-number/'))
print(q.findall('.//sequence-number/'))
print(q.findall('sequence-number'))

How can I gather this data?

CodePudding user response:

You can use the xml.etree.ElementTree module in Python to parse an XML string and extract the desired information. Here is an example of how you can do this:

import xml.etree.ElementTree as ET

# Parse the XML string
root = ET.fromstring(xml_string)

# Find all elements with the tag 'sequence-number'
sequence_numbers = root.findall('.//sequence-number')

# Extract the text from each element and add it to the list
result = [number.text for number in sequence_numbers]

This will extract all the sequence-number tags from the XML and store their text in the result list.

Note that this assumes that the XML string is well-formed and that the sequence-number tags are properly nested. If the XML is not well-formed or the tags are not properly nested, you may need to use additional logic to handle these cases.

I got the answer from openai.com

CodePudding user response:

First install lxml using pip install lxml.

Then below should help

from lxml import etree

xml_string = '''
<rpc-reply >
 <data>
  <ipv4-acl-and-prefix-list >
   <accesses>
    <access>
     <access-list-entries>
      <access-list-entry>
       <sequence-number>1</sequence-number>
      </access-list-entry>
      <access-list-entry>
       <sequence-number>10</sequence-number>
       <sequence-str>10</sequence-str>
      </access-list-entry>
     </access-list-entries>
    </access>
   </accesses>
  </ipv4-acl-and-prefix-list>
 </data>
</rpc-reply>
'''

root = etree.fromstring(xml_string)

Use the xpath function to select all sequence-number elements and store them in a list:

sequence_numbers = root.xpath('//sequence-number/text()')

The sequence_numbers list will contain the sequence numbers as strings. If you want to convert them to integers, you can use a list comprehension

sequence_numbers = [int(x) for x in sequence_numbers]

The resulting sequence_numbers list will contain the integers 1 and 10.

  • Related