Как добавить множество узлов XML и данных с условиями - PullRequest
0 голосов
/ 22 февраля 2019

Я хочу добавить некоторые элементы XML в файлы XML, используя определенные условия.Это мой xml-файл

<?xml version="1.0" encoding="UTF-8" standalone="no"?><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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.dexter</groupId>
    <artifactId>vulnTest</artifactId>
    <version>1.0</version>
    <dependencies>      
    <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.1.1.RELEASE</version>
    </dependency>       
    </dependencies>
</project>

Мне нужно проверить, доступен ли тег, называемый build, если он доступен. Мне нужно добавить его в вышеуказанный xml-файл под тегом build

<plugins>
  <plugin>
    <groupId>org.owasp</groupId>
    <artifactId>dependency-check-maven</artifactId>
    <version>4.0.2</version>
    <executions>
      <execution>
        <goals>
           <goal>check</goal>
        </goals>
      </execution>
     </executions>
  </plugin>
<plugins>

Если доступны теги build и plugins, мне нужно добавить только этот

  <plugin>
    <groupId>org.owasp</groupId>
    <artifactId>dependency-check-maven</artifactId>
    <version>4.0.2</version>
    <executions>
      <execution>
        <goals>
           <goal>check</goal>
        </goals>
      </execution>
     </executions>
  </plugin>

Если отсутствуют теги build и plugins, я хочу добавить все это с тегами build и plugins.

Я попытался проверить, существует ли тег, используя следующий код, и он работает.

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(new 
        File("C:\\Users\\Dexter\\IdeaProjects\\vulnTest\\pom.xml"));

        Node node = document.getDocumentElement();
        NodeList nl = node.getChildNodes();

        for(int i=0; i< nl.getLength(); i++){
            String tmp = nl.item(i).getNodeName();
            if (tmp.equals("build")){
                System.out.println("build Exist");

                Element ele = document.createElement("test");
                Element nodes = document.createElement("tt");
                Text txt = document.createTextNode("data");
                nodes.appendChild(txt);
                ele.appendChild(nodes);

                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.transform(new DOMSource(document), new StreamResult(new File("C:\\Users\\Dexter\\IdeaProjects\\vulnTest\\pom.xml")));

                NodeList nl2 = document.getElementsByTagName("plugins");
                for (int j=0; j<nl2.getLength();j++){
                    String tmp2 = nl2.item(j).getNodeName();
                    System.out.println(nl2.item(j).getNodeName());
                    if(tmp2.equals("plugins"))
                    {
                        System.out.println("plugins exists");
                    }
                }
            }

Я попытался добавить образец узла, так как файл выше обновляется, но узел не добавлен.

...