Update Wert des Xml-Tags mit Hilfe von Xpath in Java

Ich versuche, ändern Sie den Wert eines bestimmten Tags einer XML-Datei mit XPath in java. Ich bin versucht, dies zu erreichen mit XpathFactory aber nicht in der Lage zu.Bitte korrigieren Sie mich, wenn es gibt ein besserer Weg, dies zu tun.

JAVA-Code.

 public class MavenMetadataReader {


/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws XMLStreamException {


     Scanner user_input = new Scanner( System.in );

     String updated_pom_version;
     System.out.println("Enter updated version:");
     updated_pom_version = user_input.next( );

     File xpath=new File("D:\\Lucy\\trunk\\pom.xml");

     XPathFactory xfactory = XPathFactory.newInstance();
     XPath xpathObj = xfactory.newXPath();
     Node node;

     try {
          node = (Node)xpathObj.evaluate(xpath, doc, XPathConstants.NODE);
     } catch (XPathExpressionException e) {
          throw new RuntimeException(e);
     }

     node.setTextContent(elementValue);

Die XML-Datei..

<?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>
      <groupId>com.avocent</groupId>
      <artifactId>common-configurations</artifactId>
      <version>0.0.4</version>
   </parent>
   <groupId>com.avocent.commonplatform.cps.symbols</groupId>
   <artifactId>GDDResources</artifactId>
   <version>4.0.0.129-SNAPSHOT</version>
   <description>Resources for init data strings</description>
   <packaging>jar</packaging>
   <properties>
   <src>${basedir}</src>
   <dst>${basedir}/target/classes</dst>
 </properties>
</project>

Die zu Erwartenden Änderungen in der XML-Datei.

<?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>
      <groupId>com.avocent</groupId>
      <artifactId>common-configurations</artifactId>
      <version>0.0.4</version>
   </parent>
   <groupId>com.avocent.commonplatform.cps.symbols</groupId>
   <artifactId>GDDResources</artifactId>
   <version>3.6.10</version>    **<-- Changes are to be made in second version tag, not the first**  
   <description>Resources for init data strings</description>
   <packaging>jar</packaging>
   <properties>
   <src>${basedir}</src>
   <dst>${basedir}/target/classes</dst>
   </properties>
   </project>

Den eingegebenen Benutzer-Wert sollte neu geschrieben werden, nur der Wert des zweiten "version" - tag.Bitte Helfen Sie

  • Seine schwer zu helfen ohne zu wissen, was Sie für die variable xpath
  • Ich habe die änderungen nach, was ich in den code.
  • xpathObj.evaluate(xpath, doc, XPathConstants.NODE); -- Sie noch nicht enthalten der Wert der xpath überall auf Ihre Frage.
  • Dein XML ist nicht gültig und Sie haben ein Fehler beim analysieren es. Zu beheben, schließen Sie die <properties> tag mit </properties>.
InformationsquelleAutor Lucy | 2014-10-31
Schreibe einen Kommentar