I would like to write a script which will edit multiple XML files, I would like to have a script which will do the following;
- Find tag "Preload" delete entire tag, find "Preload=?sometext?" and delete.
- Find tag "jumpable" delete entire tag, find "jumpable=?sometext?" and delete.
- Find "tween" and delete entire tag
- Replace "slide" with "title", remove this line
- Find "offsety" and delete entire tag
- Find "offsetx" and delete entire tag
- Find "titleoffsetx" and delete entire tag
- Find "presenter" and delete entire tag
- Find "controls" and delete entire tag
- Find "demooffsetx" and "demooffsety" and delete entire tag.
- Replace "flv" with "src", unless it is ".flv"
- Remove type tag, eg. "type=?sometext?"
Before Script:
<?xml vers开发者_如何学Goion="1.0" encoding="utf-8"?>
<presentation>
<lesson>
<part src="0301p.flv" breadcrumb="This is example text1">
<cuepoints>
<cuepoint time="0:01" preload="priority" tooltip="Demo 3.1(A)" jumpable="yes">
<tween mode="instant" time="1" />
<slide flv="demos/0301d1.flv" demooffsetx="-180" demooffsety="60" type="demo"></slide>
<presenter />
<controls />
</cuepoint>
</cuepoints>
</part>
</lesson>
</presentation>
After Script:
<?xml version="1.0" encoding="utf-8"?>
<presentation>
<lesson>
<part src="0301p.flv" breadcrumb="This is example text1">
<cuepoints>
<cuepoint time="0:01" tooltip="Demo 3.1(A)">
<title src="demos/0301d1.flv"></title>
</cuepoint>
</cuepoints>
</part>
</lesson>
</presentation>
#!/usr/bin/python
import re
filename = 'editme.txt' #name of the file you want to edit
def edit(filename):
f = open(filename, 'rU') #reads your file
text = f.read() #puts everything in file into a string under var text
f.close() #closes file
text = re.sub(r'\s[Pp]reload="\w+"', '', text) #delete preload tag
text = re.sub(r'\s[Jj]umpable="\w+"', '', text) #delete jumpable tag
text = re.sub(r'<tween.+>\s+', '', text) #delete tween tag
text = re.sub(r'slide', 'title', text) #replaces slide with title tag
text = re.sub(r'\s\w+offset\w+=".+"', '', text) #delete offset/type tags
text = re.sub(r'<presenter.+>\s+', '', text) #deletes presenter tag
text = re.sub(r'\s+<controls.+>', '', text) #deletes controls tag
text = re.sub(r'<title flv', '<title src', text) #replaces flv with src in title tag
f = open(filename, 'w')
f.write(text) #replaces all text in file with the edited text
f.close()
edit(filename)
This simple python script should output what you want.
Look over PHP's SimpleXML. http://www.php.net/manual/en/simplexml.examples-basic.php
Of course, it's not your only option. Just Googling "PHP XML Parser" or "Python XML Parser" will give you a wealth of resources and libraries you can use.
Have you considered using XQuery to transform your xml data? XQuery is a language for xml transformation. In my opinion, using an XQuery script would be the best way to solve your problem. XQuery You'd just need an XQuery engine for executing your script which transforms the input xml into the output (like this one: MXQuery)
I recommend python based solution with lxml. IMHO its the best option since its very rich in functionality allowing XPath, etree or objectify, easy string parsing and tostring conversion, etc.
PHP simply does not provide you with equivalent tool-set.
精彩评论