
- Java XML Home
- Java XML Overview
- Java XML Parsers
- Java DOM Parser
- Java DOM Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java SAX Parser
- Java SAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- JDOM XML Parser
- JDOM XML Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java StAX Parser
- Java StAX Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XPath Parser
- Java XPath Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java DOM4J Parser
- Java DOM4J Parser
- Parse XML Document
- Query XML Document
- Create XML Document
- Modify XML Document
- Java XML Useful Resources
- Java XML - Questions and Answers
- Java XML - Quick Guide
- Java XML - Useful Resources
- Java XML - Discussion
Java JDOM Document getContentSize() Method
The Java JDOM getContentSize() method of Document class retrieves the number of content objects present inside an XML document. This method counts DTD declaration, processing instruction, comments and root element. It doesn't count the child elements and comments inside the root; instead, it counts the entire content of the root element as a single element.
Syntax
Following is the syntax of the Java JDOM Document getContentSize() method −
Document.getContentSize();
Parameters
The Java getContentSize() method doesn't accept any parameters.
Return Value
The Java getContentSize() method returns the number of content objects in the form of an integer.
Example 1
The following basic example creates a document and adds a root element. Using the Java JDOM Document getContentSize() method, we are going to retrieve the number of content objects.
import org.jdom2.Document; import org.jdom2.Element; public class GetContentSize { public static void main(String args[]) { try { //Creating a new Document and adding the root Document doc = new Document(); Element root = new Element("book"); doc.addContent(root); //Get the content size int content_objs = doc.getContentSize(); System.out.println("No.of content objects : "+content_objs); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays the number of content objects.
No.of content objects : 1
Example 2
The getContentSize() method returns '0' if there is no content available inside the document. The following example illustrates this scenario −
import org.jdom2.Document; public class GetContentSize { public static void main(String args[]) { try { //Creating a new document Document doc = new Document(); //Get the content size int content_objs = doc.getContentSize(); System.out.println("No.of content objects : "+content_objs); } catch (Exception e) { e.printStackTrace(); } } }
The output window displays number of content objects as '0'.
No.of content objects : 0
Example 3
Let us count the number of content objects inside the following bookstore.xml file.
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="style.xsl"?> <!DOCTYPE bookstore[ <!ELEMENT bookstore (book+)> <!ELEMENT book (name,author,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT price (#PCDATA)> ]> <!-- Information of a Bookstore (last updated 05-08-2024) --> <!-- All the books in the store are listed --> <bookstore> <book> <name>Treasure Island</name> <author>Robert Louis</author> <price>1400</price> </book> <book> <name>Oliver Twist</name> <author>Charles Dickens</author> <price>2000</price> </book> <book> <name>War and Peace</name> <author>Leo Tolstoy</author> <price>1500</price> </book> </bookstore>
The following program reads the above XML file and counts the number of content objects using getContentSize() method.
import java.io.File; import org.jdom2.Document; import org.jdom2.input.SAXBuilder; public class GetContentSize { public static void main(String args[]) { try { //Reading the document SAXBuilder saxBuilder = new SAXBuilder(); File inputFile = new File("bookstore.xml"); Document doc = saxBuilder.build(inputFile); //Get the content size int content_objs = doc.getContentSize(); System.out.println("No.of content objects : "+content_objs); } catch (Exception e) { e.printStackTrace(); } } }
The program counts 1 processing instruction, 1 DTD declaration, 2 comments and 1 root element and displays the count as 5.
No.of content objects : 5