简体   繁体   English

如何在Java中转换C#InnerText XPath方法?

[英]How to convert C# InnerText XPath method in Java?

I have the following C# method that contains an XPath query: 我有以下包含XPath查询的C#方法:

  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: 并且我必须在Java中进行转换,但是我怀疑与XPath查询有关,我已经做了类似的事情:

  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: 我的怀疑主要与此C#行有关:

 n.InnerText = valore;

in Java is correct use: 在Java中是正确的用法:

n.setText(valore);

have the same meaning? 有相同的意思?

Tnx TNX

Andrea 安德里亚

It is not so much a question of Java versus C# but rather of the API you use. 与其说Java与C#无关,不如说是您使用的API。 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 . 假设您将JDOM http://www.jdom.org/docs/apidocs/org/jdom2/Element.html#setText%28java.lang.String%29与Java结合使用,则是的,调用n.setText(valore)是等效的到C#和XmlElement / XmlNode(.NET框架中的DOM API)和n.InnerText = valore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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