fr4g3r

[XML] Edit XML file with autoit

8 posts in this topic

Hello,

this is the first time I have to edit a xml file and I need some help with that.

Here is the file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC">
<plist version="1.0">
  <dict>
    <key>showTrayIcon</key>
    <false></false>
    <key>initialized</key>
    <true></true>
    <key>boomeranged</key>
    <false></false>
    <array>
      <dict>
        <key>uIdentifier</key>
        <integer>2</integer>
        <key>type</key>
        <integer>1</integer>
        <key>bDisabled</key>
        <false></false>
        <key>bOverrideOSPolicy</key>
        <false></false>
      </dict>
    </array>
  </dict>
</plist>

Now under the <key>bDisabled</key> I want to change <false></false> to <true></true>.

I know that there is an UDF _XMLDomWrapper.au3 but I don't know how to use it.

Could someone please help me with that?

Thank you very much

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Did you try here:

 

??

 

btw.

:welcome: to the AutoIt forum.

 

 

Edited by mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

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

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

wow, that XML structure is horrible.  Is that some products XML, or something you created?

Generally, when something is related to something, it would be an attribute, or a child element like this...so you can reliably grab the element every time:

<something><enabled>true</enabled></something
<something @enabled='true' />

or at least a key value pairing...

<element @‌key='enabled' @value='true'/>

I'll give you an example working with that terrible XML in a minute.

Another edit...to actually require the script to delete and add new elements...I mean, my mind is blown at the deficiencies of this xml :).

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here you go...I implore you to create a better structure using one of the three examples I provided.

Local $oErrorHandler = ObjEvent("AutoIt.Error", "Var_ErrFunc")
$sXML = '<?xml version="1.0" encoding="UTF-8"?>' & @CRLF & _
'<plist version="1.0">' & @CRLF & _
'  <dict>' & @CRLF & _
'    <key>showTrayIcon</key>' & @CRLF & _
'    <false></false>' & @CRLF & _
'    <key>initialized</key>' & @CRLF & _
'    <true></true>' & @CRLF & _
'    <key>boomeranged</key>' & @CRLF & _
'    <false></false>' & @CRLF & _
'    <array>' & @CRLF & _
'      <dict>' & @CRLF & _
'        <key>uIdentifier</key>' & @CRLF & _
'        <integer>2</integer>' & @CRLF & _
'        <key>type</key>' & @CRLF & _
'        <integer>1</integer>' & @CRLF & _
'        <key>bDisabled</key>' & @CRLF & _
'        <false></false>' & @CRLF & _
'        <key>bOverrideOSPolicy</key>' & @CRLF & _
'        <false></false>' & @CRLF & _
'      </dict>' & @CRLF & _
'    </array>' & @CRLF & _
'  </dict>' & @CRLF & _
'</plist>'
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml($sXML)

$oKey = $oXML.SelectSingleNode("//key[text()='bDisabled']")
$oTrueOrFalse = $oKey.nextSibling

If $oTrueOrFalse.NodeName = 'false' Then
    $oTrueOrFalse.parentnode.removeChild($oTrueOrFalse)
    $oNew = $oXML.createElement('true')
    $oKey.parentNode.insertBefore($oNew,$oKey.nextSibling)
EndIf

ConsoleWrite($oKey.parentNode.xml & @CRLF)

ConsoleOutput:

<dict>
 <key>uIdentifier</key>
 <integer>2</integer>
 <key>type</key>
 <integer>1</integer>
 <key>bDisabled</key>
 <true/><key>bOverrideOSPolicy</key>
 <false></false>
</dict>

I'd do something like this:

$sXML = '<?xml version="1.0" encoding="UTF-8"?>' & @CRLF & _
'<plist version="1.0">' & @CRLF & _
'        <element key="bDisabled" value="false"/>' & @CRLF & _
'</plist>'
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadxml($sXML)

$oKey = $oXML.SelectSingleNode("//element[@key='bDisabled']")
$oKey.SetAttribute("value","true") ; but i would suggest using [1|0] instead
ConsoleWrite($oKey.parentnode.xml)

much, much, much easier AND reliable.

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.

Share this post


Link to post
Share on other sites

This could also be done with the usual regex brute way   :)

$txt = FileRead("1.xml")
$res = StringRegExpReplace($txt, '(?s).*bDisabled</key>\s+\K(\N+)', "<true></true>")
Msgbox(0,"", $res)

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hello,

first thank you for all your help!

It's a products XML, so I can't change the structure :(

Maybe it is important to know too that the key bDisabled could be several times in that file and every entry should be change to <true></true>.

In the spoiler below you can find an example of the whole xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "Something" "http://url.com/123.dtd">
<plist version="1.0">
  <dict>
    <key>showTrayIcon</key>
    <false></false>
    <key>initialized</key>
    <true></true>
    <key>boomeranged</key>
    <false></false>
    <key>proxyLocalHost</key>
    <false></false>
    <key>loggingLevel</key>
    <integer>0</integer>
    <key>nextIdentifier</key>
    <integer>4</integer>
    <key>autoUpdateTime</key>
    <integer>0</integer>
    <key>localPubringFile</key>
    <string></string>
    <key>localSecringFile</key>
    <string></string>
    <key>downloadURL</key>
    <string></string>
    <key>installer</key>
    <string></string>
    <key>userInstaller</key>
    <string></string>
    <key>win32LogWindowData</key>
    <data>123456==</data>
    <key>availableOCVersion</key>
    <data></data>
    <key>accounts</key>
    <array>
      <dict>
        <key>uIdentifier</key>
        <integer>2</integer>
        <key>type</key>
        <integer>1</integer>
        <key>bDisabled</key>
        <false></false>
        <key>bOverrideOSPolicy</key>
        <false></false>
        <key>pszTitle</key>
        <string></string>
        <key>pszAuthuser</key>
        <string></string>
        <key>pszAccountID</key>
        <string></string>
        <key>pszAddress</key>
        <string>something@something.something</string>
        <key>msIncoming</key>
        <dict>
          <key>type</key>
          <integer>1</integer>
          <key>protocol</key>
          <integer>0</integer>
          <key>dir</key>
          <integer>0</integer>
          <key>uIP</key>
          <integer>-123456</integer>
          <key>uFlags</key>
          <integer>0</integer>
          <key>usRemotePort</key>
          <integer>0</integer>
          <key>usLocalPort</key>
          <integer>0</integer>
          <key>szAddress</key>
          <string>something.something.something</string>
        </dict>
        <key>msOutgoing</key>
        <dict>
          <key>type</key>
          <integer>1</integer>
          <key>protocol</key>
          <integer>0</integer>
          <key>dir</key>
          <integer>0</integer>
          <key>uIP</key>
          <integer>-123456</integer>
          <key>uFlags</key>
          <integer>0</integer>
          <key>usRemotePort</key>
          <integer>0</integer>
          <key>usLocalPort</key>
          <integer>0</integer>
          <key>szAddress</key>
          <string>something.something.something</string>
        </dict>
        <key>ovidserver</key>
        <dict>
          <key>type</key>
          <integer>2</integer>
          <key>pszServerAddress</key>
          <string>something.something.something</string>
          <key>pszUserAddress</key>
          <string></string>
          <key>timeUpdated</key>
          <integer>123456</integer>
          <key>uServerVersion</key>
          <integer>123456</integer>
          <key>uAllowedKeyModes</key>
          <integer>123456</integer>
          <key>uKeyMode</key>
          <integer>123456</integer>
          <key>bLearnMode</key>
          <false></false>
          <key>bDirectorySyncEnabled</key>
          <true></true>
          <key>bUserDisabled</key>
          <false></false>
          <key>bAllowUserOverride</key>
          <false></false>
          <key>uKeyGenType</key>
          <integer>123456</integer>
          <key>uKeyGenSize</key>
          <integer>123456</integer>
          <key>uKeyGenFlags</key>
          <integer>0</integer>
          <key>bMinPhraseLength</key>
          <true></true>
          <key>iMinPhraseLength</key>
          <integer>123456</integer>
          <key>bMinPhraseQuality</key>
          <true></true>
          <key>iMinPhraseQuality</key>
          <integer>123456</integer>
          <key>bUseTokenForKeyGen</key>
          <false></false>
          <key>bOOMSSupport</key>
          <true></true>
          <key>pszPreferredKeyserver</key>
          <string>ldap://something.something.something</string>
          <key>keyidADK</key>
          <string>0x123456</string>
          <key>keyidOrg</key>
          <string>0x123456</string>
          <key>pbOrgCertIASN</key>
          <data></data>
          <key>palgKeyGenCiphers</key>
          <array>
            <integer>1</integer>
            <integer>2</integer>
            <integer>3</integer>
            <integer>4</integer>
            <integer>5</integer>
          </array>
          <key>palgKeyGenHashes</key>
          <array>
            <integer>6</integer>
            <integer>7</integer>
            <integer>89</integer>
          </array>
          <key>palgKeyGenCompressions</key>
          <array>
            <integer>1</integer>
            <integer>2</integer>
          </array>
          <key>ppszManagedDomains</key>
          <array>
            <string>something.something</string>
            <string>something2.something2</string>
          </array>
          <key>ppszKeysourceMaps</key>
          <array>
            <string>something.something.something=something (ldap://something.something.something:someport)</string>
            <string>something.$ADDRESS_DOMAIN=something (ldap://something.$ADDRESS_DOMAIN:someport)</string>
          </array>
          <key>bStorePassphrase</key>
          <false></false>
          <key>pszPassphrase</key>
          <string></string>
          <key>bAllowDefaultKey</key>
          <false></false>
          <key>pbEncryptedCookie</key>
          <data>something/+</data>
          <key>keyidPrivateKey</key>
          <string>0x123456</string>
          <key>bPrivateKeyReset</key>
          <false></false>
          <key>bPrivateKeyOnToken2</key>
          <false></false>
          <key>bCachePhraseAtLogon</key>
          <true></true>
          <key>pszGPXMLServerBlob</key>
          <data>something</data>
          <key>pszGPHTMLServerBlob</key>
          <data>something</data>
          <key>pszGPXMLStandaloneBlob</key>
          <data>something</data>
          <key>pszGPHTMLStandaloneBlob</key>
          <data>something</data>
          </dict>
        <key>puPolicyIDList</key>
        <array>
          <integer>-1</integer>
          <integer>-2</integer>
          <integer>-3</integer>
          <integer>-4</integer>
          <integer>-5</integer>
          <integer>-6</integer>
          <integer>-7</integer>
        </array>
        <key>pszAltDisplayNm</key>
        <string>Something, Something</string>
        <key>ppszAltIds</key>
        <array>
          <string>Something.Something@Something.Something</string>
          <string>CN=Something Something/OU=Something/O=Something@Something</string>
        </array>
      </dict>
      <dict>
        <key>uIdentifier</key>
        <integer>1</integer>
        <key>type</key>
        <integer>2</integer>
        <key>bDisabled</key>
        <false></false>
        <key>bOverrideOSPolicy</key>
        <false></false>
        <key>pszTitle</key>
        <string>Something Something</string>
        <key>pszAuthuser</key>
        <string>Something Something</string>
        <key>pszAccountID</key>
        <string>CN=Something Something/OU=Something/O=Something@Something</string>
        <key>pszAddress</key>
        <string>Something.Something@Something.Something</string>
        <key>msIncoming</key>
        <dict>
          <key>type</key>
          <integer>1</integer>
          <key>protocol</key>
          <integer>2</integer>
          <key>dir</key>
          <integer>3</integer>
          <key>uIP</key>
          <integer>4</integer>
          <key>uFlags</key>
          <integer>5</integer>
          <key>usRemotePort</key>
          <integer>6</integer>
          <key>usLocalPort</key>
          <integer>7</integer>
          <key>szAddress</key>
          <string>*</string>
        </dict>
        <key>msOutgoing</key>
        <dict>
          <key>type</key>
          <integer>1</integer>
          <key>protocol</key>
          <integer>2</integer>
          <key>dir</key>
          <integer>3</integer>
          <key>uIP</key>
          <integer>4</integer>
          <key>uFlags</key>
          <integer>5</integer>
          <key>usRemotePort</key>
          <integer>6</integer>
          <key>usLocalPort</key>
          <integer>7</integer>
          <key>szAddress</key>
          <string>*</string>
        </dict>
        <key>ovidserver</key>
        <dict>
          <key>type</key>
          <integer>1</integer>
          <key>pszServerAddress</key>
          <string>Something.Something.Something</string>
          <key>pszUserAddress</key>
          <string>Something.Something@Something.Something</string>
          <key>timeUpdated</key>
          <integer>2</integer>
          <key>uServerVersion</key>
          <integer>3</integer>
          <key>uAllowedKeyModes</key>
          <integer>4</integer>
          <key>uKeyMode</key>
          <integer>5</integer>
          <key>bLearnMode</key>
          <false></false>
          <key>bDirectorySyncEnabled</key>
          <false></false>
          <key>bUserDisabled</key>
          <false></false>
          <key>bAllowUserOverride</key>
          <false></false>
          <key>uKeyGenType</key>
          <integer>1</integer>
          <key>uKeyGenSize</key>
          <integer>2</integer>
          <key>uKeyGenFlags</key>
          <integer>3</integer>
          <key>bMinPhraseLength</key>
          <false></false>
          <key>iMinPhraseLength</key>
          <integer>4</integer>
          <key>bMinPhraseQuality</key>
          <false></false>
          <key>iMinPhraseQuality</key>
          <integer>5</integer>
          <key>bUseTokenForKeyGen</key>
          <false></false>
          <key>bOOMSSupport</key>
          <false></false>
          <key>pszPreferredKeyserver</key>
          <string></string>
          <key>keyidADK</key>
          <string></string>
          <key>keyidOrg</key>
          <string></string>
          <key>pbOrgCertIASN</key>
          <data></data>
          <key>bStorePassphrase</key>
          <false></false>
          <key>pszPassphrase</key>
          <string></string>
          <key>bAllowDefaultKey</key>
          <false></false>
          <key>pbEncryptedCookie</key>
          <data></data>
          <key>keyidPrivateKey</key>
          <string></string>
          <key>bPrivateKeyReset</key>
          <false></false>
          <key>bPrivateKeyOnToken2</key>
          <false></false>
          <key>bCachePhraseAtLogon</key>
          <false></false>
          <key>pszGPXMLServerBlob</key>
          <data></data>
          <key>pszGPHTMLServerBlob</key>
          <data></data>
          <key>pszGPXMLStandaloneBlob</key>
          <data></data>
          <key>pszGPHTMLStandaloneBlob</key>
          <data></data>
        </dict>
        <key>puPolicyIDList</key>
        <array>
          <integer>-1</integer>
          <integer>-2</integer>
          <integer>-3</integer>
          <integer>-4</integer>
          <integer>-5</integer>
          <integer>-6</integer>
          <integer>-7</integer>
        </array>
        <key>pszAltDisplayNm</key>
        <string>Something Something</string>
      </dict>
    </array>
  </dict>
</plist>

 

 

@jdelaney

 Could you give me a hint how to do that change with your code to a xml file?

You wrote the input directly to the variable $sXML, how to change that to read from file, then write to file and save it?

I tried it with 

$sXML = @ScriptDir & "\product.xml"

or

$sXML = FileRead(@ScriptDir & "\product.xml")

but both didn't work.

As you mentioned above, your code adds an element with <true/> but as the structure is given I need an <true></true>.

Could you help me with that too?

Sorry for the maybe "silly" questions, but as you maybe noticed I'm not very familiar with editing a xml in autoit. :)

 

@mikell

Thanks for your code too, I tried that with the following code but it didn't replace the key it adds a new entry at another point in the file.

$xmlfile = @ScriptDir & "\product.xml"
$txt = FileRead($xmlfile)
FileWrite($xmlfile, StringRegExpReplace($txt, '(?s).*bDisabled</key>\s+\K(\N+)', "<true></true>")

 

Edited by fr4g3r

Share this post


Link to post
Share on other sites

Ah ok, to replace several times use this
And don't forget to use the appropriate flag if you want to overwrite

#include <FileConstants.au3>

$XML = @ScriptDir & "\product.xml"
$txt = FileRead($XML)
$res = StringRegExpReplace($txt, '(bDisabled</key>\s+)(\N+)', "$1<true></true>")

$file = FileOpen($XML, $FO_OVERWRITE)  ; <<< Write mode (erase previous contents)
FileWrite($file, $res)
FileClose($file)

 

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