为什么 JDOM 的 getChild() 方法返回 null? [英] Why getChild() method of JDOM returns null?

查看:49
本文介绍了为什么 JDOM 的 getChild() 方法返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个关于 html 文档操作的项目.我希望现有 html 文档中的正文内容将其修改为新的 html.现在我正在使用 JDOM.我想在我的编码中使用 body 元素.为此我在我的编码中使用了 getChild("body").但它向我的程序返回 null.但是我的 html 文档有一个 body 元素.有人可以帮我了解这个问题吗我是学生?

I'm doing a project regarding html document manipulation. I want body content from existing html document to modify it into a new html.Now i'm using JDOM. i want to use body element in my coding.For that i used getChild("body") in my coding.But it returns null to my program.But my html document have a body element.Could anybody help me to know this problem as i'm a student?

希望得到指点..

编码:

import org.jdom.Document;
import org.jdom.Element;
public static void getBody() {
SAXBuilder builder = new SAXBuilder("org.ccil.cowan.tagsoup.Parser", true);
org.jdom.Document jdomDocument=builder.build("http://www......com");
Element root = jdomDocument.getRootElement();
      //It returns null
System.out.println(root.getChild("body"));
}

也请参考这些.. 我的 html 的根和孩子在控制台中打印...

please refer these too.. My html's root and childs printed in console...

root.getName():html

SIZE:2

[Element: <head [Namespace: http://www.w3.org/1999/xhtml]/>]

[Element: <body [Namespace: http://www.w3.org/1999/xhtml]/>]

推荐答案

我在您的代码中发现了一些问题:1) 如果您想通过网络构建远程 xml,您应该使用另一个接收 URL 作为输入的构建方法.实际上,您正在将名称为www......com"的文件解析为 xml.

I've found some problems in your code: 1) if you want to build a remote xml through the net, you should user another build method which receives an URL as input. Actually you're parsing the file with name "www......com" as an xml.

Document jdomDocument = builder.build( new URL("http://www........com"));

2) 如果你想把一个html页面解析成xml,你必须检查它是否是一个格式良好的xhtml文档,否则你不能把它解析成xml

2) if you want to parse an html page as xml, you have to check that it is a well formed xhtml document, otherwise you can't parse it as xml

3) 正如我在另一个答案中已经说过的那样,root.getChild("body") 返回 root 的孩子,名字是body",没有命名空间.您应该检查您要查找的元素的命名空间;如果它有一个合格的命名空间,你必须以这种方式传递它:

3) as I've already said you in another answer, the root.getChild("body") returns root's child which name is "body", without namespace. You should check the namespace for the element that you're looking for; if it has a qualified namespace you have to pass it in this way:

root.getChild("body", Namespace.getNamespace("your_namespace_uri"));

要以简单的方式知道哪个命名空间包含您的元素,您应该使用 getChildren 方法打印出所有根的孩子:

To know which namespace has your element in an easy way, you should print out all root's children using getChildren method:

for (Object element : doc.getRootElement().getChildren()) {
    System.out.println(element.toString());
}

如果你试图解析一个 xhtml,你可能有命名空间 uri http://www.w3.org/1999/xhtml.所以你应该这样做:

If you're trying to parse an xhtml, probably you have namespace uri http://www.w3.org/1999/xhtml. So you should do this:

root.getChild("body", Namespace.getNamespace("http://www.w3.org/1999/xhtml"));

这篇关于为什么 JDOM 的 getChild() 方法返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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