0

how to save the xml file modified by the script so that my formatting remains there?

Original pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>ArtifactParent</artifactId>
        <groupId>MaxGroup</groupId>
        <version>2.3.0</version>
    </parent>

   <artifactId>Artefact</artifactId>
   <version>1.2.3-SNAPSHOT</version>
   <packaging>pom</packaging>
   <name>This is my name</name>

   <modules>
       <module>Module1</module>
       <module>Module2</module>
   </modules>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>javax</groupId>
               <artifactId>javaee-api</artifactId>
               <version>${javaee.version}</version>
               <scope>provided</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

</project>

Purpose of script is change specific version of parent to new version. PowerShell script :

$xmlDoc = [xml] Get-Content $xmlFileName

do-something

$xmlDoc.Save($file)

When I use this script, it delete all tabulators and new-lines.

New pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>ArtifactParent</artifactId>
    <groupId>MaxGroup</groupId>
    <version>2.3.0</version>
  </parent>
  <artifactId>Artefact</artifactId>
  <version>1.2.3-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>This is my name</name>
  <modules>
    <module>Module1</module>
    <module>Module2</module>
  </modules>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>${javaee.version}</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Can you please advise me some solution which keep my formatting and also change version of parent.

4
  • It's a feature not a bug lol Commented Oct 17, 2022 at 15:48
  • @js2010 may be, but I need pom.xml look like original after updated version. Commented Oct 17, 2022 at 16:09
  • Why would you like to preserve the whitespaces? Are you having issues with, say, file comparison? Commented Oct 18, 2022 at 5:21
  • @vonPryz No, simpler solution, company policy. I need to preserve original formatting. Commented Oct 18, 2022 at 7:27

1 Answer 1

1

To prevent .Net's XML library about re-formatting the file, one needs to first tell XML Document not to mess with whitespace. This is done via creating a XmlDocument object, setting PreserveWhitespace as true and then loading the document content into it. Like so,

$xmlDoc = new-object xml.xmldocument                             
$xmlDoc.PreserveWhitespace = $true                               
$xmlDoc.Load($xmlFileName)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your advice, but when I replace this my line of conde $xmlDoc = [xml](Get-Content $xmlFileName) with your advice (exept 'pom.xml' using $xmlFileName), result is the same like New pom.xml exemple. Script formatted my formatting.
Did you also set the .PreserveWhitespace attribute before loading the content?
I found where was a problem, your solution helped me a lot, but it wasnt working until I change your $xmlDoc.Load('pom.xml') to mine $xmlDoc.Load($file), problem was in path of loading file, I dont know why with 'pom.xml' dont working but with full path in Load is workig all fine. If you want, you can edit your post and then I will mark him as solution to my problem @vonPryz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.