Jump to content

Help in one function, please!


 Share

Recommended Posts

Hello everybody!

I need help in this Function, please:

I have the following data:

<DescricaoLonga>
Deixe suas massas no ponto ideal sem muito esforço, além de contar um um exceleten cortador de talharim.

<DescricaoTecnica>
Especificações:
Cortador de Talharim
Motor:1/4 CV
Transmissão: Engrenagem


<DescricaoComposicao>
Conteúdo do Produto:
1- Cilindro Elétrico 250 com Cortador de Talharim


<DescricaoTamanho>
Dimensões aproximadas:
Rolos: 25x5 cm
Produto: 32x21x58 cm (AxLxC)


<DescricaoModoObs>
Garantia:
6 Meses

and precise that they are as follows:

<DescricaoLonga>
Deixe suas massas no ponto ideal sem muito esforço, além de contar um um exceleten cortador de talharim.
</DescricaoLonga>

<DescricaoTecnica>
Especificações:
Cortador de Talharim
Motor:1/4 CV
Transmissão: Engrenagem
</DescricaoTecnica>

<DescricaoComposicao>
Conteúdo do Produto:
1- Cilindro Elétrico 250 com Cortador de Talharim
</DescricaoComposicao>

<DescricaoTamanho>
Dimensões aproximadas:
Rolos: 25x5 cm
Produto: 32x21x58 cm (AxLxC)
</DescricaoTamanho>

<DescricaoModoObs>
Garantia:
6 Meses 
</DescricaoModoObs>

ie the first line break after open tag, he has to close the tag

please help me, I tried everything ... stringinstr, stringleft, already did several tests, but I can not =S

thankyou so much

Edited by golfinhu
Link to comment
Share on other sites

Here is my first try:

If there is an empty line preceeding the new tag then this empty line is used for the closing tag. Otherwise a line is inserted.

#include <file.au3>
#include <array.au3>

Global $aInput
_FileReadToArray("Input.txt", $aInput)
Global $Tag = ""
$iCount = 1
While 1
    If StringLeft($aInput[$iCount], 2) = "</" Then                          ; Closing tag found. Clear Tag
        $Tag = ""
    ElseIf StringLeft($aInput[$iCount], 1) = "<" Then                       ; Opening tag found
        If $Tag <> "" Then                                                  ; Old Tag not closed
            If $aInput[$iCount-1] = "" Then                                 ; Replace preceeding empty line with closing tag
                $aInput[$iCount-1] = "</" & $Tag & ">"
            Else                                                            ; insert line with closing tag if no empty line found
                _ArrayInsert($aInput,$iCount,"</" & $Tag & ">")
                $iCount += 1
            EndIf
        EndIf
        $Tag = StringTrimLeft(StringTrimRight($aInput[$iCount],1),1)        ; Extract new tag
    EndIf
    $iCount += 1
    If $iCount > UBound($aInput)-1 Then ExitLoop
WEnd
If $Tag <> "" Then _ArrayAdd($aInput,"</" & $Tag & ">")                     ; If last tag not closed then append closing tag 
$aInput[0] = UBound($aInput)-1
_ArrayDisplay($aInput)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

now thanks for your help!

it works, but I'm not getting add that to my code ... it can take a look!

I need them to add these closed tags in a file!

here's my code, and a sample file of how to be with!

; Includes
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
;===============================================================================
; Declare constant vars
;===============================================================================
Global Const $APP_DST = @TempDir & '\Jovial Xml Adapter'
Global Const $ico_app = $APP_DST & "\Document-xml.ico"
Global Const $img_fundo = $APP_DST & "\xml.jpg"
;===============================================================================
; Icon and app
;===============================================================================
DirCreate($APP_DST)
FileInstall("ico\Document-xml.ico", $ico_app, 1)
FileInstall("img\xml.jpg", $img_fundo, 1)

;===============================================================================
; Criando GUI
;===============================================================================
$Form = GUICreate("XML Adapter", 421, 236, 346, 130)
GUISetIcon($ico_app)
$Xml_Picture = GUICtrlCreatePic($img_fundo, 0, 0, 420, 100, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
$StatusBar = _GUICtrlStatusBar_Create($Form)
_GUICtrlStatusBar_SetSimple($StatusBar)
_GUICtrlStatusBar_SetText($StatusBar, "Executando...")
$Xml_Select = GUICtrlCreateInput("", 0, 144, 361, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
$Button_Convert = GUICtrlCreateButton("Converter", 173, 176, 75, 25, $WS_GROUP)
$Button_Select = GUICtrlCreateButton("...", 368, 144, 27, 17, $WS_GROUP)
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button_Select
            Global $open_file = FileOpenDialog("Select XML file", @ScriptDir, "XML files (*.xml)", 1) ;Abrir o Arquivo
            If @error Then
                MsgBox(0, '', 'Você não selecionou o arquivo xml corretamente!')

            Else
                GUICtrlSetData($Xml_Select, $open_file)
                Sleep(1000)
            EndIf


        Case $Button_Convert
            Global $file_name = GUICtrlRead($Xml_Select)
            Verificar_Arquivo()
            Subst_ponto()
    EndSwitch
WEnd


Func Verificar_Arquivo()
    Do
        Local $iMsgBoxAnswer
        Global $file_name = GUICtrlRead($Xml_Select)

        ;============================
        ;Verificar arquivo
        ;============================
        If Not FileExists($file_name) Then ;===> Se Não Existe
            MsgBox(4096, "Erro!", "Você não selecionou o arquivo corretamente!")
            _GUICtrlStatusBar_SetText($StatusBar, "Selecione o Arquivo XML!")
            $localiza_app = FileOpenDialog('Selecione o Arquivo XML', @ScriptDir, "XML files (*.xml)", 1)

            If @error Then;===> Se o Cara Clicar em Cancelar
                $iMsgBoxAnswer = MsgBox(262436, "Question?!", "Deseja Encerrar o Aplicativo?")
                Select
                    Case $iMsgBoxAnswer = 6 ;Yes
                        MsgBox(0, "...", "ok, rode o patcher novamente quando quiser!", 5)
                        Exit

                    Case $iMsgBoxAnswer = 7 ;No
                        MsgBox(0, "...", "ok, então Selecione o xml direito!", 1)
                EndSelect


            Else ;===> Se o Cara Abrir o Arquivo Normalmente!
                GUICtrlSetData($Xml_Select, $localiza_app)
                _GUICtrlStatusBar_SetText($StatusBar, "Waiting!")
            EndIf
        EndIf
    Until FileExists($file_name)
    _GUICtrlStatusBar_SetText($StatusBar, "Procurando...") ;===> Barra de Status como Procurando...
EndFunc   ;==>Verificar_Arquivo



Func Subst_ponto()
    ProgressOn("Searching...", "Removendo pontos", "0 %")
    For $subst = 1 To 9
        _ReplaceStringInFile($file_name, '<preco>R$ ' & $subst & '.', '<preco>R$ ' & $subst, 1, 1) ; Vai substituir os valores, pelos valores sem ponto
        ProgressSet($subst * 10 - 5, $subst * 10 - 5 & " %")
        _ReplaceStringInFile($file_name, '<preco_normal>R$ ' & $subst & '.', '<preco_normal>R$ ' & $subst, 1, 1) ; Vai substituir os valores, pelos valores sem ponto
        ProgressSet($subst * 10, $subst * 10 & " %")
    Next
    ProgressSet(100, "Done", "Completo")
    Sleep(500)
    ProgressOff()
    Sleep(1000)
    MsgBox(0, 'Sucesso!', 'Todos os valores foram substituidos!')
    _GUICtrlStatusBar_SetText($StatusBar, "Convertido!")
EndFunc   ;==>Subst_ponto

obs:

the function 'subst_ponto' is working properly!

it has nothing to do with what I need here now!

thanks!

the test file is attached

test.xml

Edited by golfinhu
Link to comment
Share on other sites

I inserted the convert() function and commented the Subst_ponto() function. The input file is now replaced with the converted file:

; Includes
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
;===============================================================================
; Declare constant vars
;===============================================================================
Global Const $APP_DST = @TempDir & '\Jovial Xml Adapter'
Global Const $ico_app = $APP_DST & "\Document-xml.ico"
Global Const $img_fundo = $APP_DST & "\xml.jpg"
;===============================================================================
; Icon and app
;===============================================================================
DirCreate($APP_DST)
FileInstall("ico\Document-xml.ico", $ico_app, 1)
FileInstall("img\xml.jpg", $img_fundo, 1)

;===============================================================================
; Criando GUI
;===============================================================================
$Form = GUICreate("XML Adapter", 421, 236, 346, 130)
GUISetIcon($ico_app)
$Xml_Picture = GUICtrlCreatePic($img_fundo, 0, 0, 420, 100, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
$StatusBar = _GUICtrlStatusBar_Create($Form)
_GUICtrlStatusBar_SetSimple($StatusBar)
_GUICtrlStatusBar_SetText($StatusBar, "Executando...")
$Xml_Select = GUICtrlCreateInput("", 0, 144, 361, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY))
$Button_Convert = GUICtrlCreateButton("Converter", 173, 176, 75, 25, $WS_GROUP)
$Button_Select = GUICtrlCreateButton("...", 368, 144, 27, 17, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button_Select
            Global $open_file = FileOpenDialog("Select XML file", @ScriptDir, "XML files (*.xml)", 1) ;Abrir o Arquivo
            If @error Then
                MsgBox(0, '', 'Você não selecionou o arquivo xml corretamente!')

            Else
                GUICtrlSetData($Xml_Select, $open_file)
                Sleep(1000)
            EndIf


        Case $Button_Convert
            Global $file_name = GUICtrlRead($Xml_Select)
            Verificar_Arquivo()
            ;Subst_ponto()
            Convert($file_name)
    EndSwitch
WEnd

Func Verificar_Arquivo()
    Do
        Local $iMsgBoxAnswer
        Global $file_name = GUICtrlRead($Xml_Select)

        ;============================
        ;Verificar arquivo
        ;============================
        If Not FileExists($file_name) Then ;===> Se Não Existe
            MsgBox(4096, "Erro!", "Você não selecionou o arquivo corretamente!")
            _GUICtrlStatusBar_SetText($StatusBar, "Selecione o Arquivo XML!")
            $localiza_app = FileOpenDialog('Selecione o Arquivo XML', @ScriptDir, "XML files (*.xml)", 1)

            If @error Then;===> Se o Cara Clicar em Cancelar
                $iMsgBoxAnswer = MsgBox(262436, "Question?!", "Deseja Encerrar o Aplicativo?")
                Select
                    Case $iMsgBoxAnswer = 6 ;Yes
                        MsgBox(0, "...", "ok, rode o patcher novamente quando quiser!", 5)
                        Exit

                    Case $iMsgBoxAnswer = 7 ;No
                        MsgBox(0, "...", "ok, então Selecione o xml direito!", 1)
                EndSelect


            Else ;===> Se o Cara Abrir o Arquivo Normalmente!
                GUICtrlSetData($Xml_Select, $localiza_app)
                _GUICtrlStatusBar_SetText($StatusBar, "Waiting!")
            EndIf
        EndIf
    Until FileExists($file_name)
    _GUICtrlStatusBar_SetText($StatusBar, "Procurando...") ;===> Barra de Status como Procurando...
EndFunc ;==>Verificar_Arquivo

Func Convert($InFile)

    Local $aInput
    _FileReadToArray($InFile, $aInput)
    Local $Tag = ""
    $iCount = 1
    While 1
        If StringLeft($aInput[$iCount], 2) = "</" Then ; Closing tag found. Clear Tag
            $Tag = ""
        ElseIf StringLeft($aInput[$iCount], 1) = "<" Then ; Opening tag found
            If $Tag <> "" Then ; Old Tag not closed
                If $aInput[$iCount - 1] = "" Then ; Replace preceeding empty line with closing tag
                    $aInput[$iCount - 1] = "</" & $Tag & ">"
                Else ; insert line with closing tag if no empty line found
                    _ArrayInsert($aInput, $iCount, "</" & $Tag & ">")
                    $iCount += 1
                EndIf
            EndIf
            $Tag = StringTrimLeft(StringTrimRight($aInput[$iCount], 1), 1) ; Extract new tag
        EndIf
        $iCount += 1
        If $iCount > UBound($aInput) - 1 Then ExitLoop
    WEnd
    If $Tag <> "" Then _ArrayAdd($aInput, "</" & $Tag & ">") ; If last tag not closed then append closing tag
    $aInput[0] = UBound($aInput) - 1
    _FileWriteFromArray($InFile, $aInput, 1)

EndFunc ;==>Convert

Func Subst_ponto()
    ProgressOn("Searching...", "Removendo pontos", "0 %")
    For $subst = 1 To 9
        _ReplaceStringInFile($file_name, '<preco>R$ ' & $subst & '.', '<preco>R$ ' & $subst, 1, 1) ; Vai substituir os valores, pelos valores sem ponto
        ProgressSet($subst * 10 - 5, $subst * 10 - 5 & " %")
        _ReplaceStringInFile($file_name, '<preco_normal>R$ ' & $subst & '.', '<preco_normal>R$ ' & $subst, 1, 1) ; Vai substituir os valores, pelos valores sem ponto
        ProgressSet($subst * 10, $subst * 10 & " %")
    Next
    ProgressSet(100, "Done", "Completo")
    Sleep(500)
    ProgressOff()
    Sleep(1000)
    MsgBox(0, 'Sucesso!', 'Todos os valores foram substituidos!')
    _GUICtrlStatusBar_SetText($StatusBar, "Convertido!")
EndFunc ;==>Subst_ponto

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks for your help!

I gave it a read the code, and came to understand how it was done!

very good, congratulations!

even I modified it, because the tag was only closed in the last line break, and I put pra close it in the first line break!

but i have one error in my modification =S

read the code please!

#include <file.au3>
#include <array.au3>

Global $aInput
_FileReadToArray("test.xml", $aInput) ; the file to open to array
Global $Tag = ""
$iCount = 1 ; the line of array
While 1
    If StringLeft($aInput[$iCount], 2) <> "</" And StringLeft($aInput[$iCount], 1) = "<" Then ;if line contains a opned tag!
        $Tag = StringTrimLeft(StringTrimRight($aInput[$iCount], 1), 1) ;set the tag the word that is between "< >"


        For $i = 1 To UBound($aInput) ;loop to find the first blank line
            If $aInput[$iCount + $i] = "" Then ;if the first blank line
                MsgBox (0,'','the line ' &  $iCount + $i & ' is a blank line!' & @CRLF & 'the open tag is ' & $tag & @CRLF &'and the closed tag is: ' & "</" & $Tag & ">")
                ExitLoop
            EndIf
        Next

    EndIf
    $iCount += 1
    If $iCount > UBound($aInput)-1 Then ExitLoop
WEnd
_ArrayDisplay($aInput)

it looks for the first blank line, and that line it will close the tag, everything is perfect as you can see the 'Msgbox'

but ...

when it arrives in the last tag gives an error in array, and I could not fix, I think it's in the loop 'for' 'next'

can look for me please?

again, thank you!

p.s: I'm using the attached file in the above post as a test

Edited by golfinhu
Link to comment
Share on other sites

Wow, you completely changed my code. As I don't have the time at the moment to dive into your code let's try it the other way round.

What are you going to achieve:

1) Remove all empty lines from the XML file?

2) Close all open tags?

3) Could there already be closing tags in the file?

Could you please answer this questions and then I'm going to put this into code.

Thanks

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Wow, you completely changed my code. As I don't have the time at the moment to dive into your code let's try it the other way round.

What are you going to achieve:

1) Remove all empty lines from the XML file?

2) Close all open tags?

3) Could there already be closing tags in the file?

Could you please answer this questions and then I'm going to put this into code.

Thanks

I managed to work! : D

thank so much for your help!

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...