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

  • Home
  • SEARCH
  • 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 6971579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:51:29+00:00 2026-05-27T16:51:29+00:00

My XML looks like this- <collected_objects> <object flag=complete id=objId version=1> <variable_value variable_id=varId>ValueGoesHere</variable_value> <reference item_ref=2/>

  • 0

My XML looks like this-

<collected_objects>
        <object flag="complete" id="objId" version="1">
          <variable_value variable_id="varId">ValueGoesHere</variable_value>
          <reference item_ref="2"/>
        </object>
        <object comment="objComment" flag="complete" id="objId" version="1">
          <reference item_ref="1"/>
        </object>
</collected_objects>

I am processing it using below code-

Document dom = parser.getDocument();
    NodeList collected_objects = dom.getElementsByTagName("object");
    System.out.println("Number of collected objects are " + collected_objects.getLength());

        for (int i = 0; i < collected_objects.getLength(); i++) {

            Node aNode = collected_objects.item(i);
            //get children of "objects"         
            NodeList refNodes = aNode.getChildNodes();

            System.out.println("# of chidren are " + refNodes.getLength());

            //print attributes of "objects"

            NamedNodeMap attributes = aNode.getAttributes();
            for (int a = 0; a < attributes.getLength(); a++) {
             Node theAttribute = attributes.item(a);
             System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

        }

}

it outputs as-

Number of collected objects are 2
# of chidren are 5
flag=complete
id=objId
version=1
# of chidren are 3
comment=objComment
flag=complete
id=objId
version=1

My question is why “# of chidren are” are 5 and 3 respectively? Shouldn’t I be expecting 2 and 1 respectively ?
because first object has “variable_value” and “reference” and second object has only “reference“

Essentially, my intent is to process children of “objects”.

  • 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-05-27T16:51:30+00:00Added an answer on May 27, 2026 at 4:51 pm

    That’s because you have 2 TEXT_NODE (#text) between each child nodes.

    The following included the text nodes and their corresponding values.

    <object flag="complete" id="objId" version="1">
        <TEXT_NODE />
        <variable_value variable_id="varId">ValueGoesHere</variable_value>
        <reference item_ref="2"/>
        <TEXT_NODE />
    </object>
    

    This can be verified by modifying your code:

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document dom = dBuilder.parse(new ByteArrayInputStream(S.getBytes()));
            NodeList collected_objects = dom.getElementsByTagName("object");
            System.out.println("Number of collected objects are "
                    + collected_objects.getLength());
    
            for (int i = 0; i < collected_objects.getLength(); i++) {
    
                Node aNode = collected_objects.item(i);
                // get children of "objects"
                NodeList refNodes = aNode.getChildNodes();
    
                System.out.println("# of chidren are " + refNodes.getLength());
    
                //
                for (int x = 0; x < refNodes.getLength(); x++) {
                    Node n = refNodes.item(x);
                    System.out.println(n.getNodeType() + " = " + n.getNodeName() + "/" + n.getNodeValue());
                }
    
                // print attributes of "objects"
    
                NamedNodeMap attributes = aNode.getAttributes();
                for (int a = 0; a < attributes.getLength(); a++) {
                    Node theAttribute = attributes.item(a);
                    System.out.println(theAttribute.getNodeName() + "="
                            + theAttribute.getNodeValue());
    
                }
    
            }
    

    The output:

    Number of collected objects are 2
    # of chidren are 5
    3 = #text/          
    1 = variable_value/null
    3 = #text/          
    1 = reference/null
    3 = #text/        
    flag=complete
    id=objId
    version=1
    # of chidren are 3
    3 = #text/          
    1 = reference/null
    3 = #text/        
    comment=objComment
    flag=complete
    id=objId
    version=1
    

    Where, 3 = TEXT_NODE and 1 = ELEMENT_NODE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file looks like this: <?xml version=1.0 encoding=utf-8 ?> <PathMasks> <Mask
The top of my web.xml file looks like this: <?xml version=1.0 encoding=UTF-8?> <web-app xmlns=http://java.sun.com/xml/ns/j2ee
My XML Looks like this: <?xml version=1.0 encoding=utf-8 ?> <projects> <project id=1 thumb=media/images/thumb.jpg >
My main.xml looks like this: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent >
My XML looks like this and the filename is web.config <?xml version=1.0?> <configuration> <appSettings>
So my main.xml looks like this: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent android:orientation=vertical
I am parsing XML with Javascript. My XML looks like this: <?ml version=1.0 encoding=UTF-8>
My XML looks like this: <?xml version = 1.0 encoding = utf-8?> <gallery> <name>Rosie's
My xml looks like this: <nodes><skus><sku>abc</sku><sku>def123</sku></skus></nodes> I want to get all the elements with
I want to transform an XML document. The source XML looks like this: <svc:ElementList>

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.