无法使用 Element.getAttribute() 获取元素值 [英] Failing to get element values using Element.getAttribute()

查看:36
本文介绍了无法使用 Element.getAttribute() 获取元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个 xml 文件.我找到了一个很好的例子,直到 xml 元素没有任何属性.当然,我已经尝试过如何读取属性,但它不起作用.

I would like to read an xml file. I' ve found an example which is good until the xml element doesn't have any attributes. Of course i've tried to look after how could I read attributes, but it doesn't works.

以 XML 为例

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<car>
<properties>
<test h="1.12" w="4.2">
<colour>red</colour>
</test>
</properties>
</car>

Java 代码:

public void readXML(String file) {
    try {

        File fXmlFile = new File(file);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("test : "
                        + getTagValue("test", eElement));
                System.out.println("colour : " + getTagValue("colour", eElement));

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
            .getChildNodes();


    Node nValue = (Node) nlList.item(0);
            System.out.println(nValue.hasAttributes());

    if (sTag.startsWith("test")) {
        return eElement.getAttribute("w");

    } else {
        return nValue.getNodeValue();
    }

}

输出:

测试:

颜色:红色

我的问题是,我无法打印出属性.我如何获得属性?

My problem is, that i can't print out the attributes. How could i get the attributes?

推荐答案

你的代码有很多问题;未声明的变量和看似疯狂的算法.我重写了它并且它有效:

There is alot wrong with your code; undeclared variables and a seemingly crazy algorithm. I rewrote it and it works:

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public final class LearninXmlDoc
{
    private static String getTagValue(final Element element)
    {
        System.out.println(element.getTagName() + " has attributes: " + element.hasAttributes());

        if (element.getTagName().startsWith("test"))
        {
            return element.getAttribute("w");

        }
        else
        {
            return element.getNodeValue();
        }
    }

    public static void main(String[] args)
    {
        final String fileName = "c:\\tmp\\test\\domXml.xml";

        readXML(fileName);
    }

    private static void readXML(String fileName)
    {
        Document document;
        DocumentBuilder documentBuilder;
        DocumentBuilderFactory documentBuilderFactory;
        NodeList nodeList;
        File xmlInputFile;

        try
        {
            xmlInputFile = new File(fileName);
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.parse(xmlInputFile);
            nodeList = document.getElementsByTagName("*");

            document.getDocumentElement().normalize();

            for (int index = 0; index < nodeList.getLength(); index++)
            {
                Node node = nodeList.item(index);
                if (node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element element = (Element) node;

                    System.out.println("\tcolour : " + getTagValue(element));
                    System.out.println("\ttest : " + getTagValue(element));
                    System.out.println("-----");
                }
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }
}

这篇关于无法使用 Element.getAttribute() 获取元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆