Jump to content

Search, cut and move string to different line in XML file


ceeth
 Share

Recommended Posts

Hi All,

I am new to AutoIT, but II think that it is a right tool to a job that I need to do.

I have multiple XML files, in different folders, with a structure like:

<x n="Title">""</x>
                    <x n="Label">"Random texts"</x>

 

Label strings are different throughout the file. The job is to find all those texts and move them into tag "Title", so they look like that:

<x n="Title">"Random texts"</x>
                    <x n="Label">""</x>


 I think that the below base would be a good place to start, unfortunately the move operation is problematic.

$FileContent=FileRead($File,FileGetSize($File))
$Find=InputBox("Find and replace","What to find?","FindThisText","",500,100)
$Replace=InputBox("Find and replace","What to write instead?","ReplaceWithThisText","",500,100)
 $FileContent=StringReplace($FileContent,$Find,$Replace)
 FileDelete($File)
 FileWrite($File,$FileContent)
EndIf

 

Another requirement is search rule: search should look for both lines, eg.

<x n="Title">""</x>
                    <x n="Label">"

because there are line only with  <x n="Label">" that I do not want to touch because above them there is no                 <x n="Title">""</x>. This can be done with a regexp rule:

<v n="Title">""</v>\r\n^\s*<v n="Label">"

 

What would be the correct way of cutting and pasting a string from one line to other? I would appreciate any help as I am completely new to this stuff.

Link to comment
Share on other sites

2 hours ago, ceeth said:

Hi All,

I am new to AutoIT, but II think that it is a right tool to a job that I need to do.

I have multiple XML files, in different folders, with a structure like:

<x n="Title">""</x>
                    <x n="Label">"Random texts"</x>

 

Label strings are different throughout the file. The job is to find all those texts and move them into tag "Title", so they look like that:

<x n="Title">"Random texts"</x>
                    <x n="Label">""</x>


 I think that the below base would be a good place to start, unfortunately the move operation is problematic.

$FileContent=FileRead($File,FileGetSize($File))
$Find=InputBox("Find and replace","What to find?","FindThisText","",500,100)
$Replace=InputBox("Find and replace","What to write instead?","ReplaceWithThisText","",500,100)
 $FileContent=StringReplace($FileContent,$Find,$Replace)
 FileDelete($File)
 FileWrite($File,$FileContent)
EndIf

 

Another requirement is search rule: search should look for both lines, eg.

<x n="Title">""</x>
                    <x n="Label">"

because there are line only with  <x n="Label">" that I do not want to touch because above them there is no                 <x n="Title">""</x>. This can be done with a regexp rule:

<v n="Title">""</v>\r\n^\s*<v n="Label">"

 

What would be the correct way of cutting and pasting a string from one line to other? I would appreciate any help as I am completely new to this stuff.

Ok.  So let me see if I understand your objective here.  You want to find a particular string of text that the user types in in these "label" tags, and if there is a "title" tag that precedes it before another "label" tag occurs, you want to take the text in the flagged label tag (which is what the user input), and replace the contents of the title tag with the text from the label tag (again, the text the user typed in) and leave the label tag empty.  Is that about right?

Link to comment
Share on other sites

User input could be disregarded, but may be useful if the script is to be used in other jobs. Basically, $Find Input Box should be

<x n="Title">""</x>
                    <x n="Label">"

 

After finding this data, script should move contents after "Label" to "Title". Each "Label" will have different string (or nothing).

Sorry for the confusing code, I hope it is more clear now.

Link to comment
Share on other sites

2 hours ago, ceeth said:

User input could be disregarded, but may be useful if the script is to be used in other jobs. Basically, $Find Input Box should be

<x n="Title">""</x>
                    <x n="Label">"

 

After finding this data, script should move contents after "Label" to "Title". Each "Label" will have different string (or nothing).

Sorry for the confusing code, I hope it is more clear now.

i think i understand, but to clarify, what you are trying to search for is "title" and "label" tag that can be paired with the title tag.  For example:

<x n="Title">""</x>

<x n="Control">""</x>

<x n="Label">"What are you trying to do with this, this is confusing as hell!!"</x>

 

Would that be an example of a paired title and label tag?  I guess the main unknown would be does the "label" tag have to be the very next tag after the "title" tag in order to be paired with it, or is any "label" tag automatically paired with whatever "title" tag just so happens to come before it?

Link to comment
Share on other sites

XML before modifications would look like that:

<x n="Title">""</x>

<x n="Label">"This is the part to move"</x>

 

XML after modifications:

<x n="Title">"This is the part to move"</x>

<x n="Label">""</x>

 

Link to comment
Share on other sites

1 hour ago, ceeth said:

XML before modifications would look like that:

<x n="Title">""</x>

<x n="Label">"This is the part to move"</x>

 

XML after modifications:

<x n="Title">"This is the part to move"</x>

<x n="Label">""</x>

 

Yes, so I get that part dude.  The part that I am trying to figure out is if the "label" tag which will be paired with the "title" tag is the very next declared tag after the title tag, or if there can be other tags in between the label and the title tag and the label tag would still pair with the previous title tag.  In other words, would the example I provided above still be a set of tags that would pair or not.

Link to comment
Share on other sites

2 hours ago, ceeth said:

XML before modifications would look like that:

<x n="Title">""</x>

<x n="Label">"This is the part to move"</x>

 

XML after modifications:

<x n="Title">"This is the part to move"</x>

<x n="Label">""</x>

 

You know what, I am just going to assume that the label tag comes right after the title tag, since that is the only example you will give.  Since that is the case, try this.  WARNING: Make copy of the xml file you want to use with this script.  I am not responsible for damages suffered if this warning is not considered.  Also since you for some reason don't want to supply a sample xml for me to test with, so here you go:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Language=1033
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <FileConstants.au3>
; *** End added by AutoIt3Wrapper ***
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.15.0 (Beta)
    Author:         myName

    Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <File.au3>
#include <Array.au3>

$thefile = FileOpenDialog ( "Select the XML", "", "XML Files (*.xml)", 3 )
$file = FileOpen ( $thefile, $FO_UTF8_NOBOM )
$arr = FileReadToArray ( $file )
FileClose ( $file )
_ArrayDisplay ( $arr )
$ind = _ArrayFindAll ( $arr, "Title", Default, Default, Default, 1 )
For $i = 0 To UBound ( $ind ) - 1 Step 1
    If StringInStr ( $arr[$ind[$i] + 1], "Label" ) > 0 Then
        $start = StringInStr ( $arr[$ind[$i] + 1], '"', Default, 3 )
        $end = StringInStr ( $arr[$ind[$i] + 1], '"', Default, 4 )
        $cont = StringMid ( $arr[$ind[$i] + 1], $start, ($end - $start) + 1 )
        $arr[$ind[$i]] = StringReplace ( $arr[$ind[$i]], '""', $cont )
        $arr[$ind[$i] + 1] = StringReplace ( $arr[$ind[$i] + 1], $cont, '""' )
        _FileWriteToLine ( $thefile, $ind[$i] + 1, $arr[$ind[$i]], True )
        _FileWriteToLine ( $thefile, $ind[$i] + 2, $arr[$ind[$i] + 1], True )
    Else
        ContinueLoop
    EndIf
Next

 

Edited by MattHiggs
Link to comment
Share on other sites

On 17/08/2017 at 9:48 AM, MattHiggs said:

Yes, so I get that part dude.  The part that I am trying to figure out is if the "label" tag which will be paired with the "title" tag is the very next declared tag after the title tag, or if there can be other tags in between the label and the title tag and the label tag would still pair with the previous title tag.  In other words, would the example I provided above still be a set of tags that would pair or not.

Sorry for late response. My computer crashed and I had to pause my work on XML files.

@MattHiggs Thank you very much for your help, I will test your example script asap. I did not know that you required an example XML script. The part that I have pasted is a part of an existing XML, there is not going to be anything in between - if that is what you needed to confirm. Anyway, will get back to you after tests. Thanks!

Link to comment
Share on other sites

9 minutes ago, ceeth said:

Sorry for late response. My computer crashed and I had to pause my work on XML files.

@MattHiggs Thank you very much for your help, I will test your example script asap. I did not know that you required an example XML script. The part that I have pasted is a part of an existing XML, there is not going to be anything in between - if that is what you needed to confirm. Anyway, will get back to you after tests. Thanks!

If I am going to put my official seal of approval on the script, I like to test it against an actual example.  Otherwise, I am flying blind....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...