java 为什么 JDOM 的 getChild() 方法返回 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5259321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why getChild() method of JDOM returns null?
提问by Arun
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?
我正在做一个关于 html 文档操作的项目。我希望现有 html 文档中的正文内容将其修改为新的 html。现在我正在使用 JDOM。我想在我的编码中使用 body 元素。为此我在我的编码中使用了 getChild("body")。但它向我的程序返回 null。但是我的 html 文档有一个 body 元素。有人可以帮我了解这个问题吗我是一名学生?
would appreciate pointers..
将不胜感激指针..
Coding:
编码:
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"));
}
please refer these too.. My html's root and childs printed in console...
也请参考这些.. 我的 html 的根和孩子打印在控制台...
root.getName():html
SIZE:2
[Element: <head [Namespace: http://www.w3.org/1999/xhtml]/>]
[Element: <body [Namespace: http://www.w3.org/1999/xhtml]/>]
回答by javanna
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.
我在您的代码中发现了一些问题:1) 如果您想通过网络构建远程 xml,您应该使用另一个接收 URL 作为输入的构建方法。实际上,您正在将名称为“www......com”的文件解析为 xml。
Document jdomDocument = builder.build( new URL("http://www........com"));
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
2)如果你想把一个html页面解析为xml,你必须检查它是否是一个格式良好的xhtml文档,否则无法解析为xml
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:
3)正如我在另一个答案中已经说过的那样,root.getChild("body")
返回 root 的孩子,名字是“body”,没有命名空间。您应该检查您要查找的元素的命名空间;如果它有一个合格的命名空间,你必须以这种方式传递它:
root.getChild("body", Namespace.getNamespace("your_namespace_uri"));
To know which namespace has your element in an easy way, you should print out all root's children using getChildren method:
要以简单的方式知道哪个命名空间包含您的元素,您应该使用 getChildren 方法打印出所有 root 的孩子:
for (Object element : doc.getRootElement().getChildren()) {
System.out.println(element.toString());
}
If you're trying to parse an xhtml, probably you have namespace uri http://www.w3.org/1999/xhtml
. So you should do this:
如果您正在尝试解析 xhtml,则可能您有名称空间 uri http://www.w3.org/1999/xhtml
。所以你应该这样做:
root.getChild("body", Namespace.getNamespace("http://www.w3.org/1999/xhtml"));
回答by duffymo
What makes you feel like you require org.ccil.cowan.tagsoup.Parser? What does it provide you that the parser built into the JDK does not?
是什么让你觉得你需要 org.ccil.cowan.tagsoup.Parser?它为您提供了什么而内置于 JDK 中的解析器没有提供?
I'd try it using another constructor for SAXBuilder. Use the parser built into the JDK and see if that helps.
我会尝试使用 SAXBuilder 的另一个构造函数。使用内置于 JDK 中的解析器,看看是否有帮助。
Start by printing out the entire tree using XMLOutputter.
首先使用XMLOutputter打印整个树。
public static void getBody()
{
SAXBuilder builder = new SAXBuilder(true);
Document document = builder.build("http://www......com");
XMLOutputter outputter = new XMLOutputter();
outputter.output(document, System.out); // do something w/ exception
}
回答by Arun
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", Namespace.getNamespace("my_name_space")));
}