简体   繁体   中英

How to convert C# InnerText XPath method in Java?

I have the following C# method that contains an XPath query:

  public bool setDriveParameter(DefaultDriveParameter parameter, string valore){
      System.Xml.XmlNode n;
      n = _document.SelectSingleNode("//root/settings/defaults/" + parameter.ToString().Replace("_", "-"));
      if (n == null) {
          return false;
      }

      n.InnerText = valore;

      return true;
  }

and I have to convert it in Java but I have a doubt related the XPath query, I have do something like this:

  public boolean setDriveParameter(DefaultDriveParameter parameter, String valore) {
      Element n;
      XPath xPath;

      try {
          xPath = XPath.newInstance("//root/settings/defaults/" + parameter.toString().replace("_", "-") );
          n = (Element) xPath.selectSingleNode(CONFIG_DOCUMENT);

          if (n == null) {
              return false;
          }
          n.setText(valore);

      } catch (JDOMException e) {
      }
      return true;

  }

My doubt is mainly related to this C# line:

 n.InnerText = valore;

in Java is correct use:

n.setText(valore);

have the same meaning?

Tnx

Andrea

It is not so much a question of Java versus C# but rather of the API you use. Assuming you use JDOM http://www.jdom.org/docs/apidocs/org/jdom2/Element.html#setText%28java.lang.String%29 with Java then yes, the call n.setText(valore) is equivalent to C# and XmlElement/XmlNode (the DOM API in the .NET framework) and n.InnerText = valore .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM