PHP Code:
#!/usr/bin/python
def getSpaces(line):
"""Retrieve the spaces before the first character."""
space = ""
for char in line:
if char == " " or char == "\t":
space += char
else:
break
return space
fnewdata = "newdata.xml"
fdata = "data.xml"
fexport = "data_export.xml" # In case I accidentaly break sth.
matches = {}
fnewdata = open(fnewdata, "r")
#Open newdata
for line in fnewdata:
line = line.strip()
line_key = line
#Filter
if line_key.startswith("<url>") and line_key.endswith("</info>"):
#Get the key
line_key = line_key.split("<info>")[0]
matches[line_key] = line
fnewdata.close()
fdata = open(fdata, "r")
fexport = open(fexport, "w")
for line in fdata:
#Swap'em
try:
#Create a correctly formatted string, <space><content>\n
replace = "%s%s\n" % (getSpaces(line), matches[line.strip()])
#Write it down
fexport.write(replace)
except KeyError:
#Otherwise just copy it
fexport.write(line)
fdata.close()
fexport.close()
Not the most optimal way, didn't use the xml library or regexps, just a plain if check, so it may not work well.
It also saves it as another file (data_export.xml) so you can run it safely.
(whoa, it took me 8 minutes to make this)
Bookmarks