简体   繁体   English

如何将使用XPath的这种简单C#方法转换为Java方法?

[英]How to translate this simple C# method that use XPath into a Java method?

I am pretty new in the use of XPath in Java and I don't know C# . 我在Java中使用XPath相当陌生 ,而且我不了解C#

In this time I have to convert some C# methods that use XPath into Java methods and I have some doubts about it. 这一次我必须将一些使用XPath的 C#方法转换为Java方法,对此我有些怀疑。

In a C# method I found the following lines of code: 在C#方法中,我发现了以下代码行:

System.Xml.XmlNode element;
System.Xml.XmlNode filter;

filter = _document.CreateElement("filter");
element = _document.CreateElement("name");
element.InnerText = es.Descrizione;
filter.AppendChild(element);

and I am trying to convert it into Java code. 我正在尝试将其转换为Java代码。

I think that the first 2 lines can be translated as: 我认为前两行可以翻译为:

Element element;
Element filter;

(it is simple a declaration of a Jdom Element objects) (这是Jdom Element对象的简单声明)

But I have many doubts about how translate these lines in Java: 但是我对如何在Java中转换这些行有很多疑问:

filter = _document.CreateElement("filter");
element = _document.CreateElement("name");

filter.AppendChild(element);

Can you help me? 你能帮助我吗?

Tnx TNX

Andrea 安德里亚

The Java equivalent would be the following: Java等效项如下:

import javax.xml.parsers.*;
import org.w3c.dom.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Element element;                            // System.Xml.XmlNode element;
        Element filter;                             // System.Xml.XmlNode filter;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document _document = db.newDocument();
        filter = _document.createElement("filter"); // filter = _document.CreateElement("filter");
        element = _document.createElement("name");  // element = _document.CreateElement("name");
        element.setTextContent(es.Descrizione);     // element.InnerText = es.Descrizione;
        filter.appendChild(element);                // filter.AppendChild(element);
        _document.appendChild(filter);
    }

}

Why are you mentioning XPath? 您为什么提到XPath? I don't see any use of it. 我看不到有任何用处。 If you want to continue to use the DOM API then you could do that even in Java with http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/package-summary.html and your code would be 如果您想继续使用DOM API,那么即使在Java中,也可以使用http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/package-summary.html和代码将是

import org.w3c.dom;


Element element;
Element filter;

filter = _document.createElement("filter");
element = _document.createElement("name");
element.setTextContent(es.Descrizione);
filter.appendChild(element);

If you want to use JDOM then you would need to change the approach and instead of using the DOM based factory methods like document.createElement(..) you would simply use constructor methods like new Element(..) . 如果要使用JDOM,则需要更改方法,而不是使用诸如document.createElement(..)类的基于DOM的工厂方法,而只需使用诸如new Element(..)类的构造方法。

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

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