Java 1.6.0_20中的Eclipse节点.getTextContent()

sbtkgmzw  于 2023-01-30  发布在  Eclipse
关注(0)|答案(4)|浏览(133)

我在使用org.w3c.dom的Node. getTextContent()时遇到了问题。我有以下代码块:

String name = document.getElementsByTagName("name").item(0).getTextContent();

而eclipse给了我The method getTextContent() is undefined for the type Node,我不明白是什么问题。
谢谢,问候。
编辑:

<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>FBApp</groupId>
    <artifactId>FBApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <repositories>
        <repository>
            <id>java.net.maven1</id>
            <name>java.net</name>
            <url>http://download.java.net/maven/1</url>
        </repository>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>1.2</version>
            <type>jar</type>
            <classifier>NIGHTLY_20060227</classifier>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-bean-collections</artifactId>
            <version>1.7.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-core</artifactId>
            <version>1.7.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.3</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>1.7</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-el</groupId>
            <artifactId>commons-el</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.1.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>oro</groupId>
            <artifactId>oro</artifactId>
            <version>2.0.8</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.facebookapi</groupId>
            <artifactId>facebook-java-api</artifactId>
            <version>3.0.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.facebookapi</groupId>
            <artifactId>facebook-java-api-annotation-processor</artifactId>
            <version>3.0.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.facebookapi</groupId>
            <artifactId>facebook-java-api-schema</artifactId>
            <version>3.0.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.4.2</version>
            <scope>runtime</scope>
        </dependency> 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl104-over-slf4j</artifactId>
            <version>1.4.2</version>
            <scope>runtime</scope>
        </dependency>        
    </dependencies>
</project>

这是pom.xml,commons-digester需要包含org.w3c.dom的xml-apis-1.0b2,我认为这是一个依赖性问题。

zour9fqk

zour9fqk1#

你需要在eclipse中进入项目的属性。然后选择“Java构建路径”和标签“排序和导出”。在这里你可以安排依赖关系的顺序。确保你的JRE高于Maven依赖关系。

p1tboqfb

p1tboqfb2#

尝试使用:

document.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();

AFAIK在某些情况下比getTextContent() "更安全"。

ni65a41a

ni65a41a3#

我知道这是一个较老的职位,但对于任何人谁达到这一点,我有一个类似的问题,并修复它使用的步骤中指出:
http://mergetag.com/the-method-gettextcontent-is-undefined-for-the-type-node/
也就是org.w3c.dom.Node.getTextContent() is only available as of Java 1.5,但也要注意Node的声明。xml-apis-1.0.b2.jar是有问题的一个。进入项目属性〉Java构建路径〉排序和导出标签,并将xml-apis-1.0.b2.jar移到顶部。

f4t66c6m

f4t66c6m4#

我也遇到过类似的问题,通过从JavaSDK1.11降级到1.8,我解决了这个问题

相关问题