Results 1 to 12 of 12
  1. #1
    Lounger
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Trimstart to delete all leading spaces in xml

    Hi,

    I want to remove all the leading spaces from multiple xml files.

    Before
    HTML Code:
    <test>
      <item> 
        <item> 
          <item>
    </test>

    After Trim

    <test>
    <item>
    <item>
    <item>
    </test>

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    John,

    Here's a little PowerShell program to do the trick:

    Code:
    #Version: 1.0
    
    $FileList = Get-ChildItem -Path "G:\Test" -Filter "*.xml"
    
    ForEach ($FSpec in $FileList) {
    
      $Lines = (get-content $FSpec.FullName)
    
      $Lines[1..($Lines.count -1)].TrimStart(" ") | 
      Set-Content $FSpec.FullName
    
    } # End ForEach ($FSpec...
    Results:
    StripXML.JPG

    I started with your basic file and added the Identifying information Test 1 and Test2 including spaces on either side so you could see they didn't get stripped.

    If you haven't used PowerShell you can check out THIS POST Items 1-3.

    Here's the code in a program file ready to go: Strip-XML-Leading-Spaces.zip

    Of course, you'll need to change the -Path argument to fit your computer setup.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Lounger
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi,

    Please have a look i am getting this error.

    $Lines[1..($Lines.count -1)].TrimStart(" ") |
    Method invocation failed because [System.Object[]] doesn't contain a method named 'TrimStart'.
    At C:\Test\trim.ps1:7 char:41
    + $Lines[1..($Lines.count -1)].TrimStart <<<< (" ") |
    + CategoryInfo : InvalidOperation: (TrimStart:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    John,

    In the PowerShell command window enter: $PSVersionTable this will provide the information on your version of powershell.

    Example:
    psversiontable.JPG

    Copy and paste it into your reply.
    Last edited by RetiredGeek; 2016-08-24 at 09:42.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    John,

    Try this

    Code:
    foreach($file in (Get-ChildItem -Path 'ENTER YOUR PATH HERE' -Filter '*.xml')) {
      (Get-Content -Path $file.FullName) -replace '^\s+' |
      Set-Content -Path $file.FullName
    }

  6. #6
    Lounger
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks Cliff.H your solution is working perfectly.

    Thanks RG for supporting.

    Thanks.

  7. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cliff,

    Any Idea why he got the error? It worked perfectly on my machine.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  8. #8
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    RG,

    No, I do not. I can only assume that at least one of the files read, did not return a string object, and therefore the 'TrimStart()' caused the error.

    BTW, this bit of code: $Lines[1..($Lines.count -1)].TrimStart(" ") skips the first line, so when the lines are written back to the file, the first line is missing.

  9. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cliff,

    OOPS! I modified some code that worked on .CSV files where I had to skip the headers. Your are correct it should be:
    Code:
    $Lines[0..($Lines.count -1)].TrimStart(" ")
    That could have also caused the error if there was only one line in a file!

    As always Cliff to the rescue!

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  10. #10
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cliff,

    You always get me thinking and now I've learned a new thing about get-content. Seems you don't have to iterate the lines you can just apply the method to the result as a whole (if you want to operate on the whole file that is).

    I also liked your use of the Get-ChildItem right in the ForEach vs placing it in a variable.

    Version 2:
    Code:
    #Version: 2.0
    
    # Change $Path and $Filter. Will work on ANY Text type file!
    $Path   = "G:\Test"
    $Filter = "*.xml"
    
    ForEach ($FSpec in (Get-ChildItem -Path $Path -Filter $Filter)) {
    
      (Get-Content $FSpec.FullName).TrimStart(" ") | 
       Set-Content $FSpec.FullName
    
    } # End ForEach ($FSpec...
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  11. #11
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    Would an app that does this do? I use Notepad++ to edit text files. One of its features is trimming leading spaces. There is also an XML plugin available the will pretty-print the XML (I deal a lot with XML without any linefeeds - it's all on one line).

  12. #12
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    cafed00d,

    I also use NP++ and love it. I knew you could do this there with regular expressions but I didn't know it had the feature baked in. NP++ does so many things it's hard to know all it's features. Recently, I discovered the plugin for file compare. I've since been using it to death comparing one version of a PowerShell script to another and also comparing output files from different versions to quickly verify If I've messed something w/o having to go line by line. Needless to say I'm a big NP++ fan!

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •