Jump to content

xml file manipulation


Recommended Posts

Hi everybody,

I have to export a configuration text format, this is OK.

This file have a lot of comments string, and I would like to suppress them, the syntax is <!-- "Comments" -->.

This is the first step.

Second step, is a lot of white space are present at the begin of the line, and I have to suppress them too.

I know now how to suppress a line using this script:

#include <Array.au3>
#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines, $search)
_ArrayDelete($aLines,2)
FileDelete($sFile)
For $i=1 to Ubound($aLines)-1
   If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i])
Next
Exit

But I don't know how to delete a line when a special string is present

Could you help me ? or give to me a way ?

Many thanks

serr57

Link to comment
Share on other sites

I wouldn’t call it XML manipulation, more like string parsing. If a comment begins With < ! - - "Comments" - - > find out what it ends With. This way you can get the positions of when To use String length To strip these comments.

#include <Array.au3>
#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines, $search)
_ArrayDelete($aLines, 2)
FileDelete($sFile)
For $i = 1 To UBound($aLines) - 1
    If $aLines[$i] <> "" Then
        ;Strips White Space
        $NewLine = StringStripWS($aLines[$i], 1)
        ;1 = strip leading white space
        ;2 = strip trailing white space
        ;4 = strip double (or more) spaces between words
        ;8 = strip all spaces (over-rides all other flags)
        ;
        ;-- Insert a parse for comments
        ;
        ;<!-- "Comments" --> is it? What closes comment? Syntax
        ;
        FileWriteLine($sFile, $NewLine)
    EndIf
Next
Exit

;Also find these in help for more
;StringTrimLeft
;StringTrimRight
;StringReplace
[Cheeky]Comment[/Cheeky]
Link to comment
Share on other sites

include <Array.au3>
#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines, $search)
_ArrayDelete($aLines,2)
FileDelete($sFile)
For $i=1 to Ubound($aLines)-1
   If $aLines[$i]<>"" AND StringInStr($aLines[$i],'<!-- "Comments" -->')=0 Then FileWriteLine($sFile,$aLines[$i])
Next

Link to comment
Share on other sites

Hi,

Thank you very much. I have to modify the script, and now it works here is it.

#include <Array.au3>
#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines)
_ArrayDelete($aLines,2)
FileDelete($sFile)
For $i=1 to Ubound($aLines)-1
   If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i])
   If $aLines[$i]<>"" Then FileWriteLine($sFile,$aLines[$i])
_Ltrim($sFile, $aLines)
Next

But now I have to delete white spaces at the begin of the line.

I have tried with Tim function, but it doesn't match...

Could help me ?

serr57

Link to comment
Share on other sites

Hi,

I have testing this script but some thing is wrong, I don't understand the reason why

#include <Array.au3>
#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines)
_ArrayDelete($aLines,2)
FileDelete($sFile)
For $i=1 to Ubound($aLines)-1
    If $aLines[$i]<>"" AND StringInStr($aLines[$i],'<!--')=0 Then FileWriteLine($sFile,$aLines[$i])     ;Delete all comments 
    If $aLines[$i] <> "" Then 
        $NewLine = StringStripWS($aLines[$i], 1) 
        FileWriteLine($sFile, $NewLine)     ;Delete only left white space
    EndIf
Next

Could explain to me the reason ?

Link to comment
Share on other sites

Try this:

#include <Array.au3>

#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines, $search)
_ArrayDelete($aLines,2)
FileDelete($sFile)
For $i=1 to Ubound($aLines)-1
    $aLines[$i]=StringStripWS($aLines[$i],3)
    If $aLines[$i]<>"" AND StringInStr($aLines[$i],'<!-- "Comments" -->')=0 Then FileWriteLine($sFile,$aLines[$i])
Next
Link to comment
Share on other sites

Try this:

#include <Array.au3>

#include <File.au3>

Dim $sFile = "C:\TEMP\outputfile.xml"
Dim $aLines
_FileReadToArray($sFile, $aLines, $search)
_ArrayDelete($aLines,2)
FileDelete($sFile)
For $i=1 to Ubound($aLines)-1
    $aLines[$i]=StringStripWS($aLines[$i],3)
    If $aLines[$i]<>"" AND StringInStr($aLines[$i],'<!-- "Comments" -->')=0 Then FileWriteLine($sFile,$aLines[$i])
Next

Perfect, all is working well

Many thanks for your help

serr57

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