尝试使用 .replace() 替换使用 XPATH java 提取的 XML 依赖项 [英] Trying to use .replace() to replace XML dependencies extracted by using XPATH java

查看:48
本文介绍了尝试使用 .replace() 替换使用 XPATH java 提取的 XML 依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pom xml 和 pom-web xml.我试图通过使用 Xpath 将依赖项提取到变量中并尝试使用该变量在 pom.xml 文件中执行 .replace 来将依赖项从 pom-web 复制到 pom.xml.但我无法通过变量替换内容,因为 如果 .contains() 的条件为 false.下面的任何帮助都是我使用的代码

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

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>

替换后的预期 pom.xml 输出:

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>

我使用 xpath 提取依赖的代码

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());

推荐答案

我推荐你使用 Jsoup 可以解析xml 轻松,而不是重新发明轮子.

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

将每个 xml 文件读入内存,从一个 xml 中复制依赖项节点并将其替换为另一个.

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

这是一个工作示例:

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);
 }
}

如果你使用 maven 获取 Jsoup 依赖 Maven 中心

这篇关于尝试使用 .replace() 替换使用 XPATH java 提取的 XML 依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆