Why does the getChild () JDOM method return null?

I am doing a project for processing html documents. I want the body contents from an existing html document to change it to a new html. Now I am using JDOM. I want to use the body element in my coding. For this, I used getChild ("body") in my coding. But it returns null to my program. But my html document has a body element. Could someone help me find out this problem as a student?

Pointers will be appreciated.

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 to them too .. My html root and children are printed in the console ...

root.getName():html

SIZE:2

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

[Element: <body [Namespace: http://www.w3.org/1999/xhtml]/>]
+3
source share
3 answers

: 1) xml , , URL- . "www...... com" xml.

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

2), html- xml, , xhtml, xml

3), , root.getChild("body") , "", . , ; , :

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

, , , getChildren:

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

xhtml, , uri http://www.w3.org/1999/xhtml. :

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

, org.ccil.cowan.tagsoup.Parser? , , JDK, ?

SAXBuilder. , JDK, , .

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
}
+2
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")));
}
+1
source

All Articles