Jump to content

Change file data


jfcby
 Share

Recommended Posts

Hi,

In the following xfdf file I need to be able to read and write to the <value></value>:

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="dailytotal_monday">
<value>8</value>
</field>
<field name="dailytotal_tuesday">
<value>8</value>
</field>
<field name="dailytotal_wednesday">
<value>8</value>
</field>
<field name="date_monday">
<value>10-20-2008</value>
</field>
<field name="date_tuesday">
<value>10-21-2008</value>
</field>
<field name="date_wednesday">
<value>10-22-2008</value>
</field>
<field name="emp_ss#">
<value>111-11-1111</value>
</field>
<field name="temp_employee_name">
<value>Tesing Employee Name</value>
</field>
<field name="totalweeklyhours">
<value>16</value>
</field>
</fields>
<f href="menuTEST3.pdf"/>
</xfdf>

This is the code I"m tring to get to read/write with no luck.

#include "_XMLDomWrapper.au3"

Local $sFile = "menuTEST3.xml"
If FileExists($sFile) Then
    $ret = _XMLFileOpen($sFile)
    If $ret = 0 Then Exit
    
    Dim $aKeys[1] = ["field"]
    Dim $aValues[1] = ['value'] 
Else
    MsgBox(4096, "Error", _XMLError())
EndIf

Thanks for your help,

jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Link to comment
Share on other sites

Your lacking read/write commands?

Also in-case im being an idiot, try using the normal FileRead( )/FileWrite( ) commands and see if those work.

[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Link to comment
Share on other sites

Your lacking read/write commands?

Also in-case im being an idiot, try using the normal FileRead( )/FileWrite( ) commands and see if those work.

Hi,

Thank you for your help. But, with the File Read()/File Write() example in the help file I don't know how to modify the code to read/write data between the <value></value> in my previous posted code.

Could help me to modify the code to read/write data between the <value></value>?

$file = FileOpen("test.txt", 1)

; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($file, "Line1")
FileWrite($file, "Still Line1" & @CRLF)
FileWrite($file, "Line2")

FileClose($file)

Thank you for your help,

jfcby

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

Link to comment
Share on other sites

jfcby

Example:

#include <Array.au3> ;Only for _ArrayDisplay()

;================ #Read# ============================
$sFile = @ScriptDir & "\test.txt"

$sRead = FileRead($sFile)

$aVal = StringRegExp($sRead, "(?i)<value>(.+)</value>", 3)

_ArrayDisplay($aVal)

;================ #Write# ============================
$var = 9

$sRead = StringRegExpReplace($sRead, "(?i)(<value)>(.+)<(/value>)", "\1>" & $var & "<\3")

MsgBox(0, "", $sRead)

;$hFile = FileOpen($sFile, 2)
;FileWrite($hFile, $sRead)
;FileClose($hFile)
Link to comment
Share on other sites

Back to square one again. The code the rasim provided works great except that it only list <value></value> in an array and then changes the <value></value> to one value of 9.

In my file I've shorten it to 10 values so that it will be much easier to troubleshoot the problems with the codes.

My file with the data in it is an xml that will be renamed as a xfdf when the code is working properly.

What I need to be able to do is when my script first starts up I need it to load the <value></value> in the .xml file to the correct input box in the .au3 script. Then when the script closes and with a save button update the .xml file. Below, is what I have so far.

TempStaffingTime_Example.xml

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="company_dept">
<value>Company &amp; Department</value>
</field>
<field name="company_dept_address">
<value>Company Address</value>
</field>
<field name="phone">
<value>222-222-2222</value>
</field>
<field name="company_supervisor_title">
<value>Company Supervisor Title</value>
</field>
<field name="company_supevisor">
<value>Company Supervisor</value>
</field>
<field name="temp_employee_name">
<value>Employee Name</value>
</field>
<field name="emp_ss#">
<value>111-11-1111</value>
</field>
<fields>
<field name="dailytotal_monday">
<value>8</value>
</field>
<field name="dailytotal_tuesday">
<value>8</value>
</field>
<field name="totalweeklyhours">
<value>16</value>
</field>
</fields>
<f href="temp_staffing_time_test3.pdf"/>
</xfdf>

TempStaffingTime_Example.au3

#include <GuiConstantsEx.au3>
#include <_XMLDomWrapper.au3>
#include <Array.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

$file = "TempStaffingTime_Example.xfdf"

Opt("TrayIconDebug", 1) 

; GUI
$gui = GUICreate("Temp Staffing Time", 600, 600)

; PIC
GUICtrlCreatePic("", 0, 0, 163, 68)

; LABEL
GUICtrlCreateLabel("Company Department", 5, 10, 175, 20)
GUICtrlCreateLabel("Company Address", 5, 35, 175, 20)
GUICtrlCreateLabel("Company Phone", 5, 60, 175, 20)
GUICtrlCreateLabel("Company Supervisor Title", 5, 85, 175, 20)
GUICtrlCreateLabel("Company Supervisor Name", 5, 110, 175, 20)
GUICtrlCreateLabel("Employee Name", 5, 135, 175, 20)
GUICtrlCreateLabel("Employee Social Sceurity Number", 5, 160, 175, 20)
GUICtrlCreateLabel("Daily Total Monday", 5, 185, 175, 20)
GUICtrlCreateLabel("Daily Total Tuesday", 5, 210, 175, 20)
GUICtrlCreateLabel("Total Weekly Hours", 5, 235, 175, 20)

; INPUT
$h_Input1 = GUICtrlCreateInput("", 200, 10, 130, 20)
$h_Input2 = GUICtrlCreateInput("", 200, 35, 130, 20)
$h_Input3 = GUICtrlCreateInput("", 200, 60, 130, 20)
$h_Input4 = GUICtrlCreateInput("", 200, 85, 130, 20)
$h_Input5 = GUICtrlCreateInput("", 200, 110, 130, 20)
$h_Input6 = GUICtrlCreateInput("", 200, 135, 130, 20)
$h_Input7 = GUICtrlCreateInput("", 200, 160, 130, 20)
$h_Input8 = GUICtrlCreateInput("", 200, 185, 130, 20)
$h_Input9 = GUICtrlCreateInput("", 200, 210, 130, 20)
$h_Input10 = GUICtrlCreateInput("", 200, 235, 130, 20)


; DATE
GUICtrlCreateDate("", 5, 280, 200, 20)
GUICtrlCreateLabel("(Date control expands into a calendar)", 10, 305, 200, 20)

; BUTTON
$button = GUICtrlCreateButton("Sample Button", 10, 330, 100, 30)

;LoadSettings()
GUISetState()

if FileExists($file) Then 
    MsgBox(266288,"Sample","Click the Button to load the saved settings.")
Else
    MsgBox(266288,"Sample","Change some controls and close the gui to save the settings")
EndIf

; GUI MESSAGE LOOP

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $button
            LoadSettings()
        Case $msg = $GUI_EVENT_CLOSE
            SaveSettings()
            ExitLoop
        Case Else
        ;;
    EndSelect
    
WEnd

GUIDelete()
Exit


Func SaveSettings()
    #cs
        $ofile = FileOpen($file, 2)

    ; Check if file opened for writing OK
        If $ofile = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        EndIf

        FileWrite($ofile, GUICtrlRead($h_Input1) & @CRLF)
        FileWrite($ofile, GUICtrlRead($h_Input2) & @CRLF)
        FileWrite($ofile, GUICtrlRead($h_Input3))

        FileClose($ofile)

        Return
        #ce
EndFunc  ;==>SaveSettings

Func LoadSettings() 
    If FileExists($file) Then
            $ret = _XMLFileOpen ($file)
            if $ret =0 then Exit                
            GUICtrlSetData($h_Input1, _GetFirstValue("/field/value"))           
            Return 1
    EndIf
    Return 0    
EndFunc  ;==>LoadSettings

;Get the first real value returned from the _XMLGetValue() return array.
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf
EndFunc

Determined -- Devoted -- Delivered Make your mind up -- to seriously apply yourself -- accomplishing the desired results. **** A soft answer turneth away wrath: but grievous words stir up anger. Proverbs 15:1 KJB ****

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