Skip to content

Instantly share code, notes, and snippets.

@yuriyzubarev
Created March 21, 2012 22:01
Show Gist options
  • Save yuriyzubarev/2153594 to your computer and use it in GitHub Desktop.
Save yuriyzubarev/2153594 to your computer and use it in GitHub Desktop.
Script to convert .properties files to Sun's XML property files
import os
from os.path import splitext
def xml_start():
return """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
"""
def xml_end():
return "</properties>"
def convert_single_property_to_xml(line):
entry = line.split("=", 1)
if len(entry) == 2:
return '<entry key="' + entry[0].strip() + '">' + entry[1].strip() + '</entry>\n'
else:
return ""
prop_files = [f for f in os.listdir(".") if f.endswith(".properties")]
for fname in prop_files:
xfname = splitext(fname)[0] + ".xml"
print fname + " -> " + xfname
with open(fname, "r") as fo:
with open(xfname, "w") as fxml:
fxml.write(xml_start())
for line in fo:
fxml.write(convert_single_property_to_xml(line))
fxml.write(xml_end())
fxml.close()
fo.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment