Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:09:13+00:00 2026-06-05T10:09:13+00:00

I have the following Document object – Document myDoc . myDoc holds an XML

  • 0

I have the following Document object – Document myDoc.

myDoc holds an XML file by…

myDoc = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder().parse(file);

Now I want to get the root of the XML file. Is there any difference between

Node firstChild = this.myDoc.getFirstChild() 

and

Node firstChild = (Node)myDoc.getDocumentElement()

In the first way, firstChild holds a node root of an XML file but it will not have the depth of Node. However, in the second way, firstChild will be the root with all of the depth.

For example, I have the following XML

<inventory>
    <book num="b1">
    </book>
    <book num="b2">
    </book>
    <book num="b3">
    </book>
</inventory>

and file holds it.

In the 1st case, int count = firstChild.getChildNodes() gives count = 0.

The 2nd case will give count = 3.

Am I right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:09:14+00:00Added an answer on June 5, 2026 at 10:09 am

    The Node you get using myDoc.getFirstChild() may not be the document root if there are other nodes before the document root node – such as a comment node. Look at the example below:

    import org.w3c.dom.*;
    
    public class ReadXML {
    
        public static void main(String args[]) throws Exception{     
    
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    
            // Document elements
            Document doc = docBuilder.parse(new File(args[0]));
    
            Node firstChild = doc.getFirstChild();
            System.out.println(firstChild.getChildNodes().getLength());
            System.out.println(firstChild.getNodeType());
            System.out.println(firstChild.getNodeName());
    
            Node root = doc.getDocumentElement();
            System.out.println(root.getChildNodes().getLength());
            System.out.println(root.getNodeType());
            System.out.println(root.getNodeName());
    
        }
    }
    

    When parsing the following XML file:

    <?xml version="1.0"?>
    <!-- Edited by XMLSpy -->
    <catalog>
       <product description="Cardigan Sweater" product_image="cardigan.jpg">
          <catalog_item gender="Men's">
             <item_number>QWZ5671</item_number>
             <price>39.95</price>
             <size description="Medium">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
             </size>
             <size description="Large">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
             </size>
          </catalog_item>    
       </product>
    </catalog>
    

    gives the following result:

    0
    8
    #comment
    3
    1
    catalog
    

    But if I remove the comment, it gives:

    3
    1
    catalog
    3
    1
    catalog
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML document which I would like to parse into a
I have the following XML document that I have to parse using python's minidom:
I have the following XML Document (that can be redesigned if necessary) that stores
I have the following XML document: <tt xmlns=http://www.w3.org/ns/ttml xmlns:tts=http://www.w3.org/ns/ttml#styling xml:lang=en> <head></head> <body> <div xml:lang=it>
I have following CDATA inside xml document: <![CDATA[ <p xmlns=>Refer to the below: <br/>
I have the following code and want to use it as an object. How
I have the following xml <root> <document> <account>12</account> <type name-a>stuff</type> <type name-b>stuff</type> </document> <document>
I have the following XML document: <MimeType> <Extension>.aab</Extension> <Value>application/x-authorware-</Value> </MimeType> <MimeType> <Extension>.aam</Extension> <Value>application/x-authorware-</Value> </MimeType>
I have the following xml file: <?xml version=1.0 encoding=UTF-8 standalone=no ?> <config> <a> <b>
If I have the following: var ObjectA = Ember.Object.extend({ init: function() { this._super(); document.write('init

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.