M
M
Maloyyy2021-10-22 17:30:49
Python
Maloyyy, 2021-10-22 17:30:49

How to count the number of sub-objects in Python for each XML object?

There is an XML config that needs to be written line by line to a CSV table. Each object is a rule that needs to be transferred to the table. The difficulty is that the number of Sub-Objects in each object is different. And if the Tag matches in one rule, then these values ​​will need to be written in one cell.

Below I demonstrate the beginning and end of xml. file.

<config>
<list name="TrafficRules_v2" identityCounter="777">
  <listitem>
    <variable name="Id">1</variable>
    <variable name="Order">509</variable>
    <variable name="Enabled">1</variable>
    <variable name="Color">FFCCCC</variable>
    <variable name="Name">VPN Services</variable>
    <variable name="Description">Allows access to VPN</variable>
    <variable name="IpVersion">4</variable>
    <variable name="Dst">Firewall</variable>
    <variable name="Proxy">default</variable>
    <variable name="Service">"IPsec services" "Kerio VPN" </variable>
    <variable name="ValidTime"></variable>
    <variable name="Action">PERMIT,logpkt,logconn</variable>
    <variable name="NAT">4</variable>
    <variable name="SNAT"></variable>
    <variable name="DNAT"></variable>
  </listitem>
  <listitem>
    <variable name="Id">2</variable>
    <variable name="Order">510</variable>
    <variable name="Enabled">1</variable>
    <variable name="Color">FFCCCC</variable>
    <variable name="Name">Web Services</variable>
    <variable name="Description">Allows access to HTTP and HTTPS from the Internet.</variable>
    <variable name="IpVersion">4</variable>
    <variable name="Dst">Firewall</variable>
    <variable name="Proxy">default</variable>
    <variable name="Service">"HTTP" "HTTPS" </variable>
    <variable name="ValidTime"></variable>
    <variable name="Action">PERMIT,chart,logconn</variable>
    <variable name="NAT">4</variable>
    <variable name="SNAT"></variable>
    <variable name="DNAT"></variable>
  </listitem>
  <listitem>
    <variable name="Id">3</variable>
    <variable name="Order">511</variable>
    <variable name="Enabled">1</variable>
    <variable name="Color">C9EEC6</variable>
    <variable name="Name">VPN Internet access (NAT)</variable>
    <variable name="Description">Enables access from local machines and VPN clients to the Internet using address translation.</variable>
    <variable name="IpVersion">4</variable>
    <variable name="Src">user:"6sada6d9-bc8e-0b4e-dsdx-sdsadqwd97"</variable>
    <variable name="Dst">ifgroup:"internet"</variable>
    <variable name="Proxy">default</variable>
    <variable name="Service"></variable>
    <variable name="ValidTime"></variable>
    <variable name="Action">PERMIT,chart,logconn</variable>
    <variable name="NAT">4</variable>
    <variable name="SNAT">auto</variable>
    <variable name="DNAT"></variable>
  </listitem>
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
  <listitem>
    <variable name="Id">3807</variable>
    <variable name="Order">6</variable>
    <variable name="Enabled">1</variable>
    <variable name="Color">FFFFFF</variable>
    <variable name="Name">Avay-IN</variable>
    <variable name="Description"></variable>
    <variable name="IpVersion">0</variable>
    <variable name="Src">prefix:"172.1.1.0/23"</variable>
    <variable name="Src">prefix:"192.1.1.0/23"</variable>
    <variable name="Src">prefix:"192.1.1.0/24"</variable>
    <variable name="Src">prefix:"172.1.1.0/24"</variable>
    <variable name="Dst">10.101.10.10</variable>
    <variable name="Proxy">default</variable>
    <variable name="Service"></variable>
    <variable name="ValidTime"></variable>
    <variable name="Action">PERMIT</variable>
    <variable name="NAT">4</variable>
    <variable name="SNAT"></variable>
    <variable name="DNAT"></variable>
  </listitem>
  <listitem>
    <variable name="Id">3808</variable>
    <variable name="Order">5</variable>
    <variable name="Enabled">1</variable>
    <variable name="Color">FFFFFF</variable>
    <variable name="Name">media-NET-to-PC-IN</variable>
    <variable name="Description"></variable>
    <variable name="IpVersion">0</variable>
    <variable name="Src">prefix:"192.168.1.0/24"</variable>
    <variable name="Dst">192.168.120.25</variable>
    <variable name="Proxy">default</variable>
    <variable name="Service">tcp:3389 </variable>
    <variable name="ValidTime"></variable>
    <variable name="Action">PERMIT</variable>
    <variable name="NAT">4</variable>
    <variable name="SNAT"></variable>
    <variable name="DNAT"></variable>
  </listitem>
</list>


Tell me, which library is better to use for parsing data from xml to CSV? And is there an argument to count the objects in each tree?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2021-10-22
@Maloyyy

You can do this in two passes.
On the first pass, you simply collect the set of all possible unique tags. I would simply write to the True dictionary by a key equal to the name of each tag that came across on the first pass.
As a result, you will get a dictionary with unique names in the keys, and these keys will be in the order in which they occurred in the XML (sometimes it's just convenient that they are not in random order).
Having a common set of names, you can use the standard way of writing to CSV in the form of dictionaries.
All that remains is to convert each node from your file to a dictionary. No third party libraries are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question