G
G
greatazazan2021-04-08 10:17:37
Parsing
greatazazan, 2021-04-08 10:17:37

Parsing XML files with a script. It is necessary to display the necessary information in one line, what is the easiest way to do this?

Good day. You need to parse data from xml, preferably with a script, there are a lot of files.
How can I format the following:

<fireparams>
    <fire>
      <param name="extra_ammo" value="120" />
      <param name="ammo_type" value="bullet_ar" />
      <param name="bullet_chamber" value="1" />
      <param name="helper_tp" value="weapon_term" />
      <param name="helper_fp" value="weapon_term" />
      <param name="nearmiss_signal" value="OnNearMiss" />
    </fire>
</fireparams>

parse quickly into type format:
extra_ammo=120|ammo_type=bullet_ar|bullet_chamber=1 etc.?

Those. there are not just values ​​between the opening and closing tag, which is quite easy to parse, the values ​​are stored in value=

I would be very grateful.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Karbivnichy, 2021-04-08
@greatazazan

You could at least write the language. If you can install Python and the BeautifulSoup library, then you can do it in Python:

_path = 'xml/' # путь к папке с xml файлами

def parse_xml(filename):
  with open(_path+filename) as file:
    xml = file.read()
  soup = BeautifulSoup(xml,'html.parser')

  params = soup.select('param')
  my_list = []

  for x in params:
    my_list.append(x['name']+'='+x['value'])
  return '|'.join(my_list)

file_list = os.listdir(_path)

file = open('newfile.txt','w')
for filename in file_list:	
  file.writelines(parse_xml(filename)+'\n')
file.close()

The script reads all files from the xml folder and writes them in the required format to the 'newfile.txt' file:
extra_ammo=120|ammo_type=bullet_ar|bullet_chamber=1|helper_tp=weapon_term|helper_fp=weapon_term|nearmiss_signal=OnNearMiss

X
xotkot, 2021-04-08
@xotkot

can be done in the forehead:

$ cat file.xml | awk -F'=|"' '/=/{print $3"="$6}' | tr '\n' '|' | sed 's/.$//'

but there may be different nuances depending on the xml file, it is
better to use the jq utility , more precisely yq (xq) which translates xml into a json format understandable for jq
$ cat file.xml | xq -r '.fireparams.fire.param[] | "\(."@name")=\(."@value")"' | tr '\n' '|' | sed 's/.$//'

V
Vladimir Kuts, 2021-04-08
@fox_12

In python with standard libraries:

data = '''<fireparams>
    <fire>
      <param name="extra_ammo" value="120" />
      <param name="ammo_type" value="bullet_ar" />
      <param name="bullet_chamber" value="1" />
      <param name="helper_tp" value="weapon_term" />
      <param name="helper_fp" value="weapon_term" />
      <param name="nearmiss_signal" value="OnNearMiss" />
    </fire>
</fireparams>'''

import xml.etree.ElementTree as ET

print('|'.join([f"{x.get('name')}={x.get('value')}" for x in ET.fromstring(data)[0]]))

extra_ammo=120|ammo_type=bullet_ar|bullet_chamber=1|helper_tp=weapon_term|helper_fp=weapon_term|nearmiss_signal=OnNearMiss

S
Sergey, 2021-04-08
@nozitiv4ik

You can even do this with regular expressions in notepad++:
1. Edit > Space Operations > Remove Leading and Trailing Spaces
2. Remove <fireparams> и <fire>:
Find:

<fireparams>\r\n<fire>\r\n|</fire>\r\n</fireparams>\r\n

Replace (be sure to check the box for regular expressions):
(nothing)
3. Replace the parameters:
Find:
<param name="(.*)" value="(.*)" />\r\n
Replace:
\1=\2|

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question