1

I have data, what is the best way to extra information: Below is the pesudo code. XML data:

<nf:data>
  <show>
   <ip>
    <mroute>
     <__XML__OPT_Cmd_mrib_show_mroute_command>
      <__XML__BLK_Cmd_mrib_show_mroute_command_source>
       <__XML__OPT_Cmd_mrib_show_mroute_command_source>
        <__XML__OPT_Cmd_mrib_show_mroute_command_group>
         <group>
          <__XML__PARAM_value>239.16.110.124</__XML__PARAM_value>
         </group>
        </__XML__OPT_Cmd_mrib_show_mroute_command_group>
       </__XML__OPT_Cmd_mrib_show_mroute_command_source>
      </__XML__BLK_Cmd_mrib_show_mroute_command_source>
      <__XML__OPT_Cmd_mrib_show_mroute_command_vrf>
       <__XML__OPT_Cmd_mrib_show_mroute_command___readonly__>
        <__readonly__>
         <TABLE_vrf>
          <ROW_vrf>
           <vrf-name>default</vrf-name>
           <route_count>10623</route_count>
           <star_g_cnt>4842</star_g_cnt>
           <sg_cnt>5780</sg_cnt>
           <star_g_prfx_cnt>1</star_g_prfx_cnt>
           <TABLE_one_route>
            <ROW_one_route>
             <mcast-addrs>(192.168.2.28/32, 239.16.110.124/32)</mcast-addrs>
             <pending>false</pending>
             <bidir>false</bidir>
             <uptime>3w6d</uptime>
             <mofrr>false</mofrr>
             <TABLE_mpib>
              <ROW_mpib>
               <mpib-name>pim</mpib-name>
               <oif-count>2</oif-count>
               <stale-route>false</stale-route>
              </ROW_mpib>
              <ROW_mpib>
               <mpib-name>ip</mpib-name>
               <oif-count>0</oif-count>
               <stale-route>false</stale-route>
              </ROW_mpib>
             </TABLE_mpib>
             <stats-pkts>625954</stats-pkts>
             <stats-bytes>31923654</stats-bytes>
             <stats-rate-buf>108.800 bps</stats-rate-buf>
             <route-iif>Ethernet2/1</route-iif>
             <rpf-nbr>192.168.1.251</rpf-nbr>
             <mofrr-iif>Null</mofrr-iif>
             <mofrr-nbr>0.0.0.0</mofrr-nbr>
             <internal>false</internal>
             <oif-count>2</oif-count>
             <fabric-oif>false</fabric-oif>
             <fabric-loser>false</fabric-loser>
             <TABLE_oif>
              <ROW_oif>
               <oif-name>Ethernet2/32</oif-name>
               <oif-uptime>3w6d</oif-uptime>
               <TABLE_oif_mpib>
                <ROW_oif_mpib>
                 <oif-mpib-name>pim</oif-mpib-name>
                 <stale-oif>false</stale-oif>
                 <omd-vpc-svi>false</omd-vpc-svi>
                </ROW_oif_mpib>
               </TABLE_oif_mpib>
               <rpf>false</rpf>
              </ROW_oif>
              <ROW_oif>
               <oif-name>Ethernet1/32</oif-name>
               <oif-uptime>3w6d</oif-uptime>
               <TABLE_oif_mpib>
                <ROW_oif_mpib>
                 <oif-mpib-name>pim</oif-mpib-name>
                 <stale-oif>false</stale-oif>
                 <omd-vpc-svi>false</omd-vpc-svi>
                </ROW_oif_mpib>
               </TABLE_oif_mpib>
               <rpf>false</rpf>
              </ROW_oif>
             </TABLE_oif>
            </ROW_one_route>
           </TABLE_one_route>
          </ROW_vrf>
         </TABLE_vrf>
        </__readonly__>
       </__XML__OPT_Cmd_mrib_show_mroute_command___readonly__>
      </__XML__OPT_Cmd_mrib_show_mroute_command_vrf>
     </__XML__OPT_Cmd_mrib_show_mroute_command>
    </mroute>
   </ip>
  </show>
 </nf:data>
</nf:rpc-reply>

need to parse following: 1. login to switch
2. FIND OUTINT: 3. Loop thru: (nf:data > show > ip > mroute > TABLE_vrf > ROW_vrf > TABLE_one_route > ROW_one_route) 4. pseudo code:

for loop ROW_one_route
if mcast-addrs startswith (*,)
  skip
if mcast-addrs startswith (\d)
parse following information: 
forloop TABLE_oif > ROW_oif
  statbps = stats-rate-buf
  statpkt = stats-pkts
  if oif-name startswith (vlan*) 
    skip
  if oif-name startstwith (eth)
    find oif-name
    outint.append(oif-name)

What is the best way to extract the information? Thank you for your all input.

2
  • sorry, xml2dict is buggy Commented Apr 20, 2018 at 3:23
  • Is it Dict is better then json output, to draw out each objects? Commented Apr 20, 2018 at 4:46

2 Answers 2

1

None of the following can open your xml file properly:

xml2dict, ElementTree and minidom

I went ahead of with the BeautifulSoup which I never used before, amazingly, it worked!

fp = open('test.xml')

soup = BeautifulSoup(fp, 'xml')

items =  soup.find_all('oif-name')

for item in items:
    print item
    print item.get_text()

need to parse following: nf:data > show > ip > mroute > TABLE_vrf > ROW_vrf > TABLE_one_route > ROW_one_route oif-name

The output:

<oif-name>Ethernet2/32</oif-name>
Ethernet2/32
<oif-name>Ethernet1/32</oif-name>
Ethernet1/32
Sign up to request clarification or add additional context in comments.

Comments

0

You could consider using lxml objectify. It is a module for python that allows you to deal with xml files as if they were a normal python object hierarchy. Check it out @ http://lxml.de/objectify.html.

1 Comment

Sure Brent. Is it possible to provide starting point with the current xml? thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.