Sorry if this question is already asked. I have below XML file for which i want to convert it into CSV or excel. Here i want to extract NodeName and its child DestIPAddress under IpRoutelist. and Value under Custom/Name tag
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<EnbConfigGetResponse xmlns="http://Airspan.Netspan.WebServices">
<EnbConfigGetResult>
<ErrorCode>OK</ErrorCode>
<NodeResult>
<NodeResultCode>OK</NodeResultCode>
<NodeName>IMUMB0899</NodeName>
<NodeDetail>
<Custom>
<Name>Circle</Name>
<Value>MU</Value>
</Custom>
<Custom>
<Name>GW VLAN 601</Name>
<Value>2405:200:101::</Value>
</Custom>
<Custom>
<Name>GW VLAN 602</Name>
<Value>2405:200:104::</Value>
</Custom>
</NodeDetail>
<EnbConfig>
<Name>IMUMB0899</Name>
<Hardware>1000 SFP</Hardware>
<Description>TT</Description>
<Site>DND</Site>
<Region>DND</Region>
<Altitude>0</Altitude>
<NbifEventAlarmForwarding>Enabled</NbifEventAlarmForwarding>
<ENodeBType>Macro</ENodeBType>
<ENodeBID>397063</ENodeBID>
<M1SubnetMask>120</M1SubnetMask>
<IpRouteList>
<IpRoute>
<DestIpAddress>172.172.6.20</DestIpAddress>
<IpSubnetMask>255.255.255.255</IpSubnetMask>
<GatewayIpAddress>172.21.200.1</GatewayIpAddress>
</IpRoute>
<IpRoute>
<DestIpAddress>2405:20:1::</DestIpAddress>
<IpSubnetMask>40</IpSubnetMask>
<GatewayIpAddress>2405:20:101:4:7:2:61:1</GatewayIpAddress>
</IpRoute>
</IpRouteList>
<NodeResult>
</EnbConfigGetResult>
</EnbConfigGetResponse>
</soap:Body>
</soap:Envelope>
I tried below code which extract the Name and IProute, but when i try to merge tham only one IP route i get against NodeName but two are available.
from bs4 import BeautifulSoup
import pandas as pd
import lxml
import xml.etree.cElementTree
import openpyxl
import inspect
import os
sites = "xml"
with open(sites, "r",encoding='unicode_escape') as f:
xml_data = f.read()
soup = BeautifulSoup(xml_data, "xml")
tag1 = input("Enter tagname1:")
tag2 = input("Enter tagname2:")
data = []
dd = []
for td in soup.find_all(tag1):
data.append({"NodeName": td.text})
for iproute in soup.find_all(tag2):
dd.append({"IpRoute": iproute.text})
df1 = pd.DataFrame(data)
df2 = pd.DataFrame(dd)
df = pd.merge(df1,df2,left_index=True, right_index=True)
df.to_excel(sites + '.xlsx', sheet_name='Detail', index = False)
print("*************Done*************")

tag1andtag2? Also, why are you storing your results in two separate data structures (dataanddd), then merging them later? Don't you want to keep everything together?