Skip to content
Advertisement

Trying to use .replace() to replace XML dependencies extracted by using XPATH java

i have a pom xml and pom-web xml. im trying to copy the dependency from pom-web to pom.xml by extracting the dependency into a variable using Xpath and trying to do .replace in the pom.xml file with the variable. but im not able to replace the content via the variable as the if condition for .contains() is false. Any help below is the code i used

pom.xml

<project>
    <dependencies>
        <dependency>
            <groupId>com.test.tm</groupId>
            <artifactId>gameJdk</artifactId>
            <version>1.0.0.1</version>
        </dependency>
        <!-- For Compress JS -->
        <dependency>
            <groupId>com.yahoo.platform.yui</groupId>
            <artifactId>yuicompressor</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>EccpressoFIPSJca</artifactId>
            <version>7.0</version>
        </dependency>

        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>EccpressoFIPS</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

pom-web.xml

<project>
    <dependencies>
        <dependency>
            <groupId>com</groupId>
            <artifactId>passwordsdk</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- For Compress JS -->
        <dependency>
            <groupId>com.yahoo.platform.yui</groupId>
            <artifactId>yuicompressor</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>EccpressoFIPSJca</artifactId>
            <version>7.0</version>
        </dependency>

        <dependency>
            <groupId>com.data</groupId>
            <artifactId>EccpressoFIPS</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>tesdt</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

Expected pom.xml output after replace:

<project>
    <dependencies>
        <dependency>
            <groupId>com</groupId>
            <artifactId>passwordsdk</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- For Compress JS -->
        <dependency>
            <groupId>com.yahoo.platform.yui</groupId>
            <artifactId>yuicompressor</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>EccpressoFIPSJca</artifactId>
            <version>7.0</version>
        </dependency>

        <dependency>
            <groupId>com.data</groupId>
            <artifactId>EccpressoFIPS</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>tesdt</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

my code for extracting the Dependency using using xpath

import java.io.StringWriter;
import java.nio.file.Files;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.slf4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public static void main (String args[]){
        





 Path c1=Paths.get(prop.getProperty("testPom"));
            Path c2=Paths.get(prop.getProperty("testPomweb"));
            //String pom = readFile(CfoServerModifications.class.getResourceAsStream(prop.getProperty("testPom")));
            //String web = readFile(CfoServerModifications.class.getResourceAsStream(prop.getProperty("testPomweb")));            
            String pomFile = new String(Files.readAllBytes(c1),StandardCharsets.UTF_8);
            String pomWeb = new String(Files.readAllBytes(c2),StandardCharsets.UTF_8);
            Document doc_web = Jsoup.parse(pomWeb,"",Parser.xmlParser());
            Document doc_pom = Jsoup.parse(pomFile,"",Parser.xmlParser());
            doc_pom.outputSettings().prettyPrint(false);
            doc_web.outputSettings().prettyPrint(false);
           // System.out.println(doc_pom.outputSettings().prettyPrint(false));
            Elements dependencies_web = doc_web.select("project>dependencies");
            Elements dependencies_pom = doc_pom.select("project>dependencies");
            //log.info(dependencies_web.toString());
            // remove the old dependencies
            dependencies_pom.clear();
            //log.info("pom clear"+dependencies_pom.toString());
            //add the new dependencies
            dependencies_pom.addAll(dependencies_web);
            //log.info("pom web added to pom "+dependencies_pom.toString());
           
            Files.write(Paths.get(prop.getProperty("dummyfile")),doc_pom.toString().getBytes());

Advertisement

Answer

I recommend you use Jsoup which can parse xml easily and not reinvent the wheel.

Read each xml file to memory, copy the dependencies node from one xml and replace them with the other’s.

Here is a working example :

package replacexmlnode;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;


public class ReplaceXmlNode {
    
  public static void main(String[] args) throws IOException {
    
    String pom = readFile(ReplaceNode.class.getResourceAsStream("pom-plain.xml"));
    String web = readFile(ReplaceNode.class.getResourceAsStream("pom-web.xml"));
    
    Document doc_web = Jsoup.parse(web,"",Parser.xmlParser());
    Document doc_pom = Jsoup.parse(pom,"",Parser.xmlParser());
    
    Elements dependencies_web = doc_web.select("project>dependencies");

    //remove old dependencies        
    doc_pom.select("project>dependencies").remove();

    // add new dependencies
    doc_pom.select("project").first().appendChild(dependencies_web.first());       
    
    doc_pom.outputSettings().prettyPrint(false);
    Files.write(Paths.get("pom-plain_out.xml"), doc_pom.toString().getBytes());
 }

  public static String readFile(InputStream in) throws IOException{
   
    return new String(in.readAllBytes(), StandardCharsets.UTF_8);
 }
}

If you use maven get Jsoup depency from maven central

9 People found this is helpful
Advertisement