Jump to content

Edit XML TAG Contents and Decimal Places


Go to solution Solved by mikell,

Recommended Posts

Hi guys,

I am hoping someone can help me with automating editing XML files using AUTOIT. I have some XML for example:-

 <TotalPayments>265.52</TotalPayments>

  <CostOfMaterials>0.10</CostOfMaterials>
  <TotalDeducted>331.92</TotalDeducted>
 
 

And I need to edit all occurences of the above tags to change the pence values to '00'. So I would want this to be changed into:-

 <TotalPayments>265.00</TotalPayments>

  <CostOfMaterials>0.00</CostOfMaterials>
  <TotalDeducted>331.00</TotalDeducted>

 

Any help appreciated.

Link to comment
Share on other sites

  • Moderators

Fraz,

Welcome to the AutoIt forum. :)

Just the thing for a RegEx: ;)

$sString = "<TotalPayments>265.52</TotalPayments>" & @CRLF & _
    "<CostOfMaterials>0.10</CostOfMaterials>" & @CRLF & _
    "<TotalDeducted>331.92</TotalDeducted>"

$sModdedString = StringRegExpReplace($sString, "(?=.)(\d\d)(?=<)", "00")

MsgBox(0, "Modded", $sModdedString)
SRER decode:

(?=.)   - Must be preceded by .
(\d\d)  - Must be 2 digits (this is the capturing group)
(?=<)   - Must be followed by < 

00      - If all of the above are true, replace the 2 digits with 00
All good? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi M23,

Wow your fast ;)

just a quick question though instead of loading :-

$sString = "<TotalPayments>265.52</TotalPayments>" & @CRLF & _
    "<CostOfMaterials>0.10</CostOfMaterials>" & @CRLF & _
    "<TotalDeducted>331.92</TotalDeducted>"

 

Should I not be loading my xml file path? Also their could be multiple <TotalPayments> tags would this code handle them all?

Thanks again M23.

Link to comment
Share on other sites

  • Moderators

Fraz,

You will need to read the file into a variable using FileRead and put that variable into the RegEx in place of $sString. :)

And the code will handle all double digit instances preceded by a "." and followed by a "<" - that is one of the wonders of a RegEx. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

XMLDOM route:

#include <File.au3>
$oXML = ObjCreate("Microsoft.XMLDOM")

$sXML_File = @DesktopDir & "\temp.xml"
$sSampleXML = "<SomeParent><TotalPayments>265.52</TotalPayments><CostOfMaterials>0.10</CostOfMaterials><TotalDeducted>331.92</TotalDeducted></SomeParent>"
_FileCreate($sXML_File)
FileWrite($sXML_File,$sSampleXML)

$oXML.load($sXML_File)
$oTotalPayments     = $oXML.selectSingleNode("//TotalPayments")
$oCostOfMaterials   = $oXML.selectSingleNode("//CostOfMaterials")
$oTotalDeducted     = $oXML.selectSingleNode("//TotalDeducted")

$oTotalPayments.text    = StringFormat('%.2f',Floor($oTotalPayments.text))
$oCostOfMaterials.text  = StringFormat('%.2f',Floor($oCostOfMaterials.text))
$oTotalDeducted.text    = StringFormat('%.2f',Floor($oTotalDeducted.text))

; or, use regexpreplace
;~ $oTotalPayments.text = StringRegExpReplace($oTotalPayments.text,"(\.\d{2})",".00")
;~ $oCostOfMaterials.text   = StringRegExpReplace($oCostOfMaterials.text,"(\.\d{2})",".00")
;~ $oTotalDeducted.text = StringRegExpReplace($oTotalDeducted.text,"(\.\d{2})",".00")

ConsoleWrite($oXML.xml & @CRLF)
$oXML.save($sXML_File)

output:

<SomeParent><TotalPayments>265.00</TotalPayments><CostOfMaterials>0.00</CostOfMaterials><TotalDeducted>331.00</TotalDeducted></SomeParent>

another option of looping through the nodes of a parent node

#include <File.au3>
$oXML = ObjCreate("Microsoft.XMLDOM")

$sXML_File = @DesktopDir & "\temp.xml"
$sSampleXML = "<SomeParent><TotalPayments>265.52</TotalPayments><CostOfMaterials>0.10</CostOfMaterials><TotalDeducted>331.92</TotalDeducted></SomeParent>"
_FileCreate($sXML_File)
FileWrite($sXML_File,$sSampleXML)

$oXML.load($sXML_File)

$oNodes = $oXML.selectNodes("/SomeParent/*")
For $oNode In $oNodes
    $oNode.text = StringFormat('%.2f',Floor($oNode.text))
Next
ConsoleWrite($oXML.xml & @CRLF)
$oXML.save($sXML_File)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Hi JDelaney,

Thanks for the solution, I have tried your solution but seem to be going wrong somewhere, I have changed some code so that :-

2.xml is the output XML file

1.xml is my original XML file

I added in a FileRead but couldnt get it to work, but have since taken it out of the below code.

But the program doesnt create the new xml file successfully :(

My altered code:-

#include <File.au3>
#include <_XMLDomWrapper.au3>
$oXML = ObjCreate("Microsoft.XMLDOM")

$sXML_File = "c:\Fraz\SAP\AutoIT\2.xml"
$sSampleXML = "c:\Fraz\SAP\AutoIT\1.xml"
_FileCreate($sXML_File)
FileWrite($sXML_File,$sSampleXML)

$oXML.load($sXML_File)
$oTotalPayments     = $oXML.selectSingleNode("//TotalPayments")
$oCostOfMaterials   = $oXML.selectSingleNode("//CostOfMaterials")
$oTotalDeducted     = $oXML.selectSingleNode("//TotalDeducted")

$oTotalPayments.text    = StringFormat('%.2f',Floor($oTotalPayments.text))
$oCostOfMaterials.text  = StringFormat('%.2f',Floor($oCostOfMaterials.text))
$oTotalDeducted.text    = StringFormat('%.2f',Floor($oTotalDeducted.text))

; or, use regexpreplace
;~ $oTotalPayments.text = StringRegExpReplace($oTotalPayments.text,"(\.\d{2})",".00")
;~ $oCostOfMaterials.text   = StringRegExpReplace($oCostOfMaterials.text,"(\.\d{2})",".00")
;~ $oTotalDeducted.text = StringRegExpReplace($oTotalDeducted.text,"(\.\d{2})",".00")

ConsoleWrite($oXML.xml & @CRLF)
$oXML.save($sXML_File)
Edited by Fraz
Link to comment
Share on other sites

Melba,

Typo ... positive look-behind (?<=.) ;)

Obviously depending on the rest of the text, this should work

$sModdedString = StringRegExpReplace($sString, "\.\d+", ".00")

Edit

Being very selective would need

$sModdedString = StringRegExpReplace($sString, "(TotalPayments|CostOfMaterials|TotalDeducted)(>\d+)\.\d+", "$1$2.00")
Edited by mikell
Link to comment
Share on other sites

Hi Mikell,

I think the solutions kindly provided by M23 and JDelaney work, but i just cant get the script to work with my existing xml file which needs to be changed.

So i need to find out how to change JDelaney's script so that I can get the script to change the existing xml file.

Thanks,

Fraz

Link to comment
Share on other sites

There is no need for the file write.  The data in the file is not changed until you do the xml.save...so, you can load in the second file, and save to the first file.  Without the xml, I can't help you debug...does this output anything?  If not, it's not valid xml:

ConsoleWrite($oXML.xml & @CRLF)
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Hi jdelaney,
 
After changing the sample xml to $sSampleXML = "c:FrazSAPAutoIT1.xml"
 
The output error is :- C:FrazSAPAutoITXML EDIT.au3 (15) : ==> The requested action with this object has failed.:
$oTotalPayments.text    = StringFormat('%.2f',Floor($oTotalPayments.text))
$oTotalPayments.text    = StringFormat('%.2f',Floor($oTotalPayments.text^ ERROR
>Exit code: 1    Time: 0.236

The new XML File is created (2.xml) but its completely empty.
 
The XML file that i am trying to edit is attached.
 
Thanks,
 
Fraz

1.XML

Edited by Fraz
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...