Document class for Jsoup Learning

Source: Internet
Author: User
Tags baseuri
Document Class 1: Class Structure org. jsoup. nodesClassDocumentjava. lang. Objectorg. jsoup. nodes. Nodeorg. jsoup. nodes. Elementorg. jsoup. nodes. DocumentAllImplementedInterfaces: Cloneable-

Document class

I. Class Structure

Org. jsoup. nodes
Class Document

Java. lang. Object

Org. jsoup. nodes. Node

Org. jsoup. nodes. Element

Org. jsoup. nodes. Document

All Implemented Interfaces:

Cloneable


--------------------------------------------------------------------------------

Public class Document

Extends

Element

An HTML document

Ii. Method

Constructor Summary
 
Document (String baseUri)
Constructor: Creates a blank HTML document based on baseUri.
 
 
Method Summary
 
Element
Body ()
Obtain the body content of an HTML document
 
Document
Clone ()
Create a full copy of the Document
 
Element
CreateElement (String tagName)
Create a tagName Node
 
Static Document
CreateShell (String baseUri)
Create a blank HTML framework
 
Element
Head ()
Get the head value of Document
 
String
NodeName ()
Obtains the node name of a node.
 
Document
Normalise ()
Normalize document
 
String
OuterHtml ()
Get the node's HTML
 
Document. OutputSettings
OutputSettings ()
Get the document's current output settings.
 
Document
OutputSettings (Document. OutputSettings outputSettings)
Set the document's output settings.
 
Document. QuirksMode
QuirksMode ()


 
Document
QuirksMode (Document. QuirksMode quirksMode)


 
Element
Text (String text)
Obtains the text value of a node or Document.
 
String
Title ()

Obtain the Document title Value
 
Void
Title (String title)
Set the Document title Value
 


Iii. Instances


[Java] view plaincopyprint?
Import org. jsoup. Jsoup;
Import org. jsoup. nodes. Document;
Import org. jsoup. nodes. Element;

Public class JsoupDocument {
Private static Document doc;
Private static Document shell;
Public static void main (String [] args ){
Try {
Doc = Jsoup. connect ("http://www.baidu.com"). get ();
// System. out. println (doc );
// Body ();
// Clone ();
// CreateElement ();
// CreateShell ();
// Head ();
// NodeName ();
// OuterHtml ();
// Text ("hello ");
// Text ();
// Title ();
Title ("Enoch Eliot ");
} Catch (Exception e ){
// TODO Auto-generatedcatch block
E. printStackTrace ();
}
}
// Obtain the body
Private static void Body (){
System. out. println (doc. body ());
}
// Clone Document
Private static void Clone (){
Document clone = doc. clone ();
System. out. println (clone );
}
// Create a node, for example:
Private static void CreateElement (){
Element e = doc. createElement ("div ");
System. out. println (e );
}
// Create an overall framework
/*
Output:




*/
Private static void CreateShell (){
Shell = Document. createShell ("http://www.baidu.com ");
System. out. println (shell );
}
// Obtain the Head
/*
* Output:
*

Baidu, you will know

Script functionh (obj) {obj. style. behavior = 'url (# default # homepage) '; vara = obj. setHomePage ('HTTP: // www.baidu.com/');} script

**/
Private static void Head (){
Element e = doc. head ();
System. out. println (e );
}
// Obtain the node name
Private static void NodeName (){
String name = doc. body (). nodeName ();
System. out. println (name );
Name = doc. body (). getElementById ("content"). nodeName ();
System. out. println (name );
Name = doc. body (). getElementById ("content"). getElementById ("u"). nodeName ();
System. out. println (name );
}
// Obtain the node's html
/*
* Output:
*
Search Settings |
Login Registration

**/
Private static void OuterHtml (){
String html = doc. body (). getElementById ("content"). getElementById ("u"). outerHtml ();
// Element html = doc. body (). getElementById ("content"). getElementById ("u ");
System. out. println (html );
}
// Set the text value of the node or Document
// When the text value of the Document is set, all nodes in the body will be deleted.
Private static void Text (String text ){
// Set the text of the node
Element e = doc. body (). getElementById ("content"). getElementById ("u"). select ("a"). get (0). text (text );
System. out. println (e );
// Set the text value of the Document
Doc. text (text );
System. out. println (doc );
}
// Obtain the text value of a node or Document
Private static void Text (){
String text = doc. body (). getElementById ("content"). getElementById ("u"). select ("a"). get (0). text ();
System. out. println (text );
}
// Set the Document title Value
Private static void Title (String title ){
Doc. title (title );
System. out. println (doc );
}
// Obtain the title value of Document
Private static void Title (){
String text = doc. title ();
System. out. println (text );
}
}

Import org. jsoup. Jsoup;
Import org. jsoup. nodes. Document;
Import org. jsoup. nodes. Element;
 
Public class JsoupDocument {
Private static Document doc;
Private static Document shell;
Public static void main (String [] args ){
Try {
Doc = Jsoup. connect ("http://www.baidu.com"). get ();
// System. out. println (doc );
// Body ();
// Clone ();
// CreateElement ();
// CreateShell ();
// Head ();
// NodeName ();
// OuterHtml ();
// Text ("hello ");
// Text ();
// Title ();
Title ("Enoch Eliot ");
} Catch (Exception e ){
// TODO Auto-generatedcatch block
E. printStackTrace ();
}
}
// Obtain the body
Private static void Body (){
System. out. println (doc. body ());
}
// Clone Document
Private static void Clone (){
Document clone = doc. clone ();
System. out. println (clone );
}
// Create a node, for example:
Private static void CreateElement (){
Element e = doc. createElement ("div ");
System. out. println (e );
}
// Create an overall framework
/*
Output:



 
*/
Private static void CreateShell (){
Shell = Document. createShell ("http://www.baidu.com ");
System. out. println (shell );
}
// Obtain the Head
/*
* Output:
*

Baidu, you will know

Script functionh (obj) {obj. style. behavior = 'url (# default # homepage) '; vara = obj. setHomePage ('HTTP: // www.baidu.com/');} script

**/
Private static void Head (){
Element e = doc. head ();
System. out. println (e );
}
// Obtain the node name
Private static void NodeName (){
String name = doc. body (). nodeName ();
System. out. println (name );
Name = doc. body (). getElementById ("content"). nodeName ();
System. out. println (name );
Name = doc. body (). getElementById ("content"). getElementById ("u"). nodeName ();
System. out. println (name );
}
// Obtain the node's html
/*
* Output:
*
Search Settings |
Login Registration

**/
Private static void OuterHtml (){
String html = doc. body (). getElementById ("content"). getElementById ("u"). outerHtml ();
// Element html = doc. body (). getElementById ("content"). getElementById ("u ");
System. out. println (html );
}
// Set the text value of the node or Document
// When the text value of the Document is set, all nodes in the body will be deleted.
Private static void Text (String text ){
// Set the text of the node
Element e = doc. body (). getElementById ("content"). getElementById ("u"). select ("a"). get (0). text (text );
System. out. println (e );
// Set the text value of the Document
Doc. text (text );
System. out. println (doc );
}
// Obtain the text value of a node or Document
Private static void Text (){
String text = doc. body (). getElementById ("content"). getElementById ("u"). select ("a"). get (0). text ();
System. out. println (text );
}
// Set the Document title Value
Private static void Title (String title ){
Doc. title (title );
System. out. println (doc );
}
// Obtain the title value of Document
Private static void Title (){
String text = doc. title ();
System. out. println (text );
}
}
Iv. Summary

The Document class is mainly used for HTML documents, including obtaining the body, header Head, overall framework Shell, Title, and Text of HTML documents (actually body content), HTML Document copying. The main body of the function is Document. You can change the Text value of a node in the Title, text, and HTML documents.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.