Jump to content

Read a .xml file, find a certain character sequence and make a new line.


xero
 Share

Recommended Posts

Ok, i've tried everything I can think of and its now giving me a headache... This is the problem.

<line1>hello</line1><line2>hello</line2><line3>hello</line3>

I have this, for example, in my .xml file. What I wish to do is put each one on a separate line. Like this:

<line1>hello</line1>
<line2>hello</line2>
<line3>hello</line3>

Bearing in mind that I have hundreds of these files, and each one has hundreds of lines in like this, I don't fancy doing it by hand! I've tried various ideas like using FileRead and FileWrite, StringReplace, yet nothing seems to allow me to put a new line in. I had slight success using the example in the help file under the FileWrite explanation;

$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)

Which works brilliantly, but I can't seem to get it to read the file and insert the new line where it is supposed to be.

Sorry if this makes no sense, i'm trying my hardest to understand how this all works!

Link to comment
Share on other sites

  • Moderators

If you post a test file to work with, I can see if I can make a quick working example so we aren't chasing our tails :) .

Edit:

Going off your current example, I did this quickly:

$sString = "<line1>hello</line1><line2>hello</line2><line3>hello</line3>"
$sNewString = StringReplace($sString, '><', '>' & Chr(1) & '<');Putting a delimeter for the split
$aSplit = StringSplit($sNewString, Chr(1))
If Not FileExists('ExampleTest.txt') Then FileClose(FileOpen('ExampleTest.txt', 2));Create the file if it doesn't exist where the script is being ran from
;Now write the split
For $iCC = 1 To UBound($aSplit) - 1;Looping the array we created with StringSplit
    FileWriteLine('ExampleTest.txt', $aSplit[$iCC])
Next
;DoneoÝ÷ ØGb·`'túèm¦åÉ©Þjȯ²¶hÖ®¶­sbb33c·57G&ærÒgV÷C²fÇC¶ÆæSfwC¶VÆÆòfÇC²öÆæSfwC²fÇC¶ÆæS"fwC¶VÆÆòfÇC²öÆæS"fwC²fÇC¶ÆæS2fwC¶VÆÆòfÇC²öÆæS2fwC²gV÷C°¢b33c·4æWu7G&ærÒ7G&æu&WÆ6Rb33c·57G&ærÂb33²fwC²fÇC²b33²Âb33²fwC²b33²fײ5$Äbfײb33²fÇC²b33²µWGFærFVÆÖWFW"f÷"FR7Æ@¤bæ÷BfÆTW7G2b33´W×ÆUFW7BçGBb33²FVâfÆT6Æ÷6RfÆT÷Vâb33´W×ÆUFW7BçGBb33²Â"´7&VFRFRfÆRbBFöW6âb33·BW7BvW&RFR67&B2&Vær&âg&öФfÆUw&FRb33´W×ÆUFW7BçGBb33²Âb33c·4æWu7G&ær
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

<Line1>Test</Line1><Line2>Test</Line2><Line3>Test</Line3><Line4>Test</Line4>

Put that into a notepad and save as test.xml will create the same file as I am testing with... I'm not sure how to upload the file itself anywhere i'm afraid :/

Sorry i'm not very helpful! ^^

Link to comment
Share on other sites

  • Moderators

<Line1>Test</Line1><Line2>Test</Line2><Line3>Test</Line3><Line4>Test</Line4>

Put that into a notepad and save as test.xml will create the same file as I am testing with... I'm not sure how to upload the file itself anywhere i'm afraid :/

Sorry i'm not very helpful! ^^

I've given 2 examples above that worked for me.

You'll obviously replace the quoted string you provided with $sString = FileRead('FileName.xml')

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I've given 2 examples above that worked for me.

You'll obviously replace the quoted string you provided with $sString = FileRead('FileName.xml')

Ah yeah, I must of been typing my reply while you edited your post.. I'll test it out now, thanks very much :)

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