GNGNUT

Reading writing TXT.xml file

5 posts in this topic

#1 ·  Posted (edited)

Hi Guys

need some pointing in the right direction here,

have a script I'm trying to write , which Requires reading a txt document (Xml) obtaining the line number of (Said) nodes   (Policyholder, Driver)

and writing a line based on information win in a tag ,

 

 

problem I'm getting when using "@error" is 1 = File has less lines than $iLine file contains 140 line and the line Im trying to write to is 40

have attached test file of what I'm looking at

basically what I need to do is If town node is blank look at county 1st and get the data from there else look at locality and take that data and place it in the Town node,

any advice would be great ,

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#Include <Excel.au3>
#include <Date.au3>

HotKeySet("{ESC}", "Terminate")
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

#AutoIt3Wrapper_run_debug_mode=Y

;~###################################################################
;;#########   LIVE   ########
;~  $Filelocation = 
;~  $Movefile = 
;~###################################################################
;~ ############  TESTING   #######
    $Filelocation = "S:\Development Share\Craig\file\"  ; Test location
    $Movefile = $Filelocation&"Moved\"   ; test location
;~###################################################################

$Wsinsertamend = _FileListToArray($Filelocation ,"*.xml", 1)
if @error Then
    msgbox(1,"Warning...","No files currently In the folder")
    exit
EndIf

for $Filearray = 1 to $Wsinsertamend[0]  ; number of files in the folder - for each file do the actions
    $result =_FileCountLines($Filelocation&$Wsinsertamend[$Filearray]) ;; count the lines within the file
    $PolicyholderNode=0
    $DriverNode=0
    $count=0
    $counting=0


    While $DriverNode = 0
        $count=$count+1
        $Driver = FileReadLine($Filelocation & $Wsinsertamend[$Filearray],$count)
        If  StringInStr ($Driver,"<PolicyHolder>") Then ;; get the line policyholder Node is on
            $PolicyholderNode = $Count
        endif
        if StringInStr ($Driver,"<Driver>") Then  ;; get the line Driver Node is on
            $DriverNode = $Count
        endif
    wend    ; max of 50 lines read

    msgbox(0,"while end",$DriverNode)

            $RLine2 = FileReadLine ($Filelocation & $Wsinsertamend[$Filearray],2)
            if StringInStr ($RLine2,"<Error>") then
                If StringInStr($RLine2, "<Error> PolicyHolderTown  Invalid</Error>") then
                    $PHLocality = FileReadLine ($Filelocation & $Wsinsertamend[$Filearray],$PolicyholderNode+9)
                    $PHCounty = FileReadLine ($Filelocation & $Wsinsertamend[$Filearray],$PolicyholderNode+11)

                    If StringInStr ($PHCounty,"<County />") then
                        If StringInStr ($PHLocality,"<Locality />") then
                        ;; Leave the file if both are Blank Manual Action to be taken
                        else
                            $Cutline  =  StringSplit($PHLocality,">") ; value 2 for the midle text
                            $Cutline  =  StringSplit($Cutline[2],"<") ; value 1
                            _FileWriteToLine( $Filelocation&$Wsinsertamend,$PolicyholderNode+10,"          <Town>"&$Cutline[1]&"</Town>",1)

                            if @error then
                                Msgbox(0,"Error" ,"Error Number - "& @error & @CRLF & "NO. lines - " & $result & @crlf & "line writing to " & $PolicyholderNode+10 & @CRLF & "text to be writen - " &  "        <Town>"&$Cutline[1]&"</Town>")
                            EndIf

                            _FileWriteToLine( $Filelocation&$Wsinsertamend,2,"",1) ;; Clear error

                            if @error then
                                Msgbox(0,"PH Locality - Line 2 " , @error)
                            EndIf
                        endif
                    Else
                        $Cutline  =  StringSplit($PHCounty,">") ; value 2 for the midle text
                        $Cutline  =  StringSplit($Cutline[2],"<") ; value 1
                        _FileWriteToLine( $Filelocation&$Wsinsertamend,$PolicyholderNode+10,"          <Town>"&$Cutline[1]&"</Town>",1)

                        if @error then
                            Msgbox(0,"Error" ,"Error Number - "& @error & @CRLF & "NO. lines - " & $result & @crlf & "line writing to " & $PolicyholderNode+10 & @CRLF & "text to be writen - " &   "       <Town>"&$Cutline[1]&"</Town>")
                        EndIf

                        _FileWriteToLine( $Filelocation&$Wsinsertamend,2,"",1) ;; Clear error
                        if @error then
                            Msgbox(0,"PH County - Line 2 " , @error)
                        EndIf
                    endif
                endif
            Else
            $error = 1
            Endif
next

 

Thanks for taking a look,

kind regards

Craig

Test data file.xml

Edited by GNGNUT

Share this post


Link to post
Share on other sites



I know may not suit your purpose but having done something similar - I took a different take for you to ponder:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <Excel.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>

HotKeySet("{ESC}", "Terminate")
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

#AutoIt3Wrapper_run_debug_mode=n

;~###################################################################
;;#########   LIVE   ########
;~  $Filelocation =
;~  $Movefile =
;~###################################################################
;~ ############  TESTING   #######
$Filelocation = "S:\Development Share\Craig\file\" ; Test location
$Movefile = $Filelocation & "Moved\" ; test location
;~###################################################################

$Wsinsertamend = _FileListToArray($Filelocation, "*.xml", 1)
If @error Then
    MsgBox(1, "Warning...", "No files currently In the folder")
    Exit
EndIf
_ArrayDisplay($Wsinsertamend)

$farray = FileReadToArray($Filelocation & $Wsinsertamend[1])
_ArrayDisplay($farray)

$sSearch = '<County'
If @error Then Exit

Local $iIndex = _ArraySearch($farray, $sSearch, 0, 0, 0, 1)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Not Found", '"' & $sSearch & '" was not found in the array.')
Else
    MsgBox($MB_SYSTEMMODAL, "Found", '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".")
EndIf

If StringRegExp($farray[$iIndex], '>[a-z0-9].*<') = 1 Then
    ConsoleWrite($farray[$iIndex] & @CRLF)
Else
    ConsoleWrite($farray[$iIndex - 1] & @CRLF & $farray[$iIndex - 2] & @CRLF)
EndIf

_ArraySwap($farray[$iIndex],  $farray[$iIndex - 2])

_ArrayDisplay($farray)

 

Share this post


Link to post
Share on other sites

Thank you for your reply and a new method in getting the data, but this still doesn't help me with my situation,

 

problem I'm getting when using "@error" -  1 = ("File has less lines than $iLine") - file contains 140 line and the line I'm trying to write to is 40

 

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hello, I think the path you are writing to may be incorrect because it contains an array with no index value.  You should add some debugging to check all your paths:

_FileWriteToLine( $Filelocation&$Wsinsertamend,2,"",1) ;; Clear error

$Wsinsertamend is an array.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

OMG - How did I not see that HAHAHA

 

Thank you for the fresh eyes  JFISH :):)

_FileWriteToLine( $Filelocation&$Wsinsertamend,2,"",1)

should Be

_FileWriteToLine( $Filelocation&$Wsinsertamend[$a],2,"",1)

 

Issue resolved :):)

 

Share this post


Link to post
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