Answer the question
In order to leave comments, you need to log in
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>
Answer the question
In order to leave comments, you need to log in
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()
extra_ammo=120|ammo_type=bullet_ar|bullet_chamber=1|helper_tp=weapon_term|helper_fp=weapon_term|nearmiss_signal=OnNearMiss
can be done in the forehead:
$ cat file.xml | awk -F'=|"' '/=/{print $3"="$6}' | tr '\n' '|' | sed 's/.$//'
$ cat file.xml | xq -r '.fireparams.fire.param[] | "\(."@name")=\(."@value")"' | tr '\n' '|' | sed 's/.$//'
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
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
<param name="(.*)" value="(.*)" />\r\n
\1=\2|
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question