Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add root node to xml
#1
Hi,
I generated xml string and want to add root node:

#lets create a dataframe
df=pd.DataFrame({'A':[1,2,3],'B':['a','b','c']})

def to_xml(df):
    def row_xml(row):
        xml = ['<item>']
        for i, col_name in enumerate(row.index):
            xml.append('  <{0}>{1}</{0}>'.format(col_name, row.iloc[i]))
        xml.append('</item>')
        return '\n'.join(xml)
    res = '\n'.join(df.apply(row_xml, axis=1))
    return(res)
1
to_xml(df)
<item>
<A>1</A>
<B>a</B
</item>
<item>
<A>2</A>
<B>b</B>
</item>
<item>
<A>3</A>
<B>c</B>
</item>
...
========================
Add root node:


<Header>
     <Name></Names>
     <Release><</software>
<Header>

<Values>
    <item>
       <A>1</A>
       <B>a</B
   </item>
   <item>
       <A>2</A>
       <B>b</B>
   </item>
   <item>
       <A>3</A>
       <B>c</B>
   </item>
<Values>
Reply


Messages In This Thread
Add root node to xml - by SriRajesh - Oct-15-2022, 02:20 PM
RE: Add root node to xml - by ndc85430 - Oct-15-2022, 02:26 PM
RE: Add root node to xml - by SriRajesh - Oct-15-2022, 02:29 PM
RE: Add root node to xml - by snippsat - Oct-16-2022, 11:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to pass a input after changing the user from root to non root using python avinash 3 3,318 Apr-08-2019, 10:05 AM
Last Post: avinash

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020