为了账号安全,请及时绑定邮箱和手机立即绑定

parse方法 输出为null

Document d = db.parse(new File("C:\\Users\\Lenovo\\Desktop\\demo.xml"));

System.out.println(d);

[#document: null]

正在回答

1 回答

我在API里看了一下Document(org.w3c.dom.Document)发现它是继承Node,而Node下的各个接口(interface)有对应的节点名称(nodeName)、节点值(nodeValue)以及属性(attributes)。

https://img1.sycdn.imooc.com//5d22fdc500012bbd12130332.jpg

而Document对应的节点名称为"#document",其节点值得到的最终还是null,属性为null。

如果只是将代码中Document的对象直接输出,那么会显示对应的名称以及对应的值。

注:原本调用System.out.println()方法应该是输出Object类中的toString()方法定义的:

      getClass().getName() + '@' + Integer.toHexString(hashCode())

但是这里输出的:

    [#document: null]

应该是某个父类(估计是NodeImpl类或者其他某个类)中重写的toString()方法

public String toString() {
    return "[" + getNodeName() + ": " + getNodeValue() + "]";
}

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

parse方法 输出为null

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信