I have to develop a script that converts an XML file exported from a MySql database, to one that can be imported into another database. I am limited with the export, by MySql, and with the import by the structure of the larger database.
A lot of it has already been solved, but I am stuck. I am new at PythonScript, I am sure it can be done, but I am having no luck. I have been through everything here, found topics that gave me ideas, but I still can't get this part of the script to work properly.
This is what I am trying to do. My XML file has been fixed partially by editor.rereplace and editor.deleteLine etc. It looks like this, in part (after the script hs done the bits that work):
Now I have a lot of </column> tags, each needs to change to the closing tag that matches the respective opening tag. So for the <SessionId> lines I need </SessionId> at the end, but for the lines that begin <ClientId> I need </Column> to change to </ClientId>. I've tried a bunch of different methods. This is the last thing I tried, based on another topic here, but what it actually did was delete lines 4 to 13. I cut the contents of the function out, replaced them one line at a time, and now although all I did was cut/paste out and back, it does nothing at all....
and I call it like this:
endColumnReplace('<SessionId>', '</column>', '</SessionId>')
but what it does is nothing. I don't think there is a way to step through the script to see what it is doing and where it is going so I am at my wits end trying to figure out what I have done wrong. How can I make it work?Thank you :)
Added: Sorry, I made a couple of silly errors when I was typing this last night. I have fixed them. I hope lol
Last edit: Andie P 2016-06-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It's OK I think, I am getting there. It does the first one correctly now, it just goes a bit stupid afterwards but that's progress lol; I am thinking its probably only a very minor error...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
editor.gotoPos(0) #goes to beginning of document
editor.searchAnchor() #sets the search anchor to the beginning of document
last_found_position = editor.searchNext(0, startText) #searches for the next occurrence of startText (<SessionId>)
while last_found_position != -1:
last_found_line = editor.lineFromPosition(last_found_position) #retrieves the line number where startText was found
editor.gotoLine(last_found_line) #and goes to that line
editor.searchAnchor() #sets the search anchor to that line
replace_position = editor.searchNext(0, textToFind) #searches for the text to be replaced
editor.setTargetStart(replace_position)
editor.setTargetEnd(replace_position + 9)
editor.replaceTarget(newText) #replaces the unwanted text
editor.gotoLine(last_found_line + 1) #moves to the next line and starts the search again
editor.searchAnchor()
last_found_position = editor.searchNext(0, startText)
return
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The python script has a console in which you can output too. Not sure if you knew that, but that's always a good place to start. Also anytime I run across a module I am not sure about, I run a help() command from the console on it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have to develop a script that converts an XML file exported from a MySql database, to one that can be imported into another database. I am limited with the export, by MySql, and with the import by the structure of the larger database.
A lot of it has already been solved, but I am stuck. I am new at PythonScript, I am sure it can be done, but I am having no luck. I have been through everything here, found topics that gave me ideas, but I still can't get this part of the script to work properly.
This is what I am trying to do. My XML file has been fixed partially by editor.rereplace and editor.deleteLine etc. It looks like this, in part (after the script hs done the bits that work):
<DEXFileUpload>
etc etc etc
Now I have a lot of </column> tags, each needs to change to the closing tag that matches the respective opening tag. So for the <SessionId> lines I need </SessionId> at the end, but for the lines that begin <ClientId> I need </Column> to change to </ClientId>. I've tried a bunch of different methods. This is the last thing I tried, based on another topic here, but what it actually did was delete lines 4 to 13. I cut the contents of the function out, replaced them one line at a time, and now although all I did was cut/paste out and back, it does nothing at all....
This is my function as it stands now:
def endColumnReplace(startText, textToFind, newText):
and I call it like this:
endColumnReplace('<SessionId>', '</column>', '</SessionId>')
but what it does is nothing. I don't think there is a way to step through the script to see what it is doing and where it is going so I am at my wits end trying to figure out what I have done wrong. How can I make it work?Thank you :)
Added: Sorry, I made a couple of silly errors when I was typing this last night. I have fixed them. I hope lol
Last edit: Andie P 2016-06-23
It's OK I think, I am getting there. It does the first one correctly now, it just goes a bit stupid afterwards but that's progress lol; I am thinking its probably only a very minor error...
In case this can help someone else at some time, I got it working myself... here is the correct function:
def endColumnReplace(startText, textToFind, newText):
Andie:
The python script has a console in which you can output too. Not sure if you knew that, but that's always a good place to start. Also anytime I run across a module I am not sure about, I run a help() command from the console on it.