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 6124157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:06:02+00:00 2026-05-23T16:06:02+00:00

I am using DOM representations in java how can I distinguish if an xml

  • 0

I am using DOM representations in java

how can I distinguish if an xml tag has a value inside it or has another embedded tag ?
For example, I can have :

<item> 2 </item>

or

<item> <name> item1 </name> </item>

i want to do the following

if(condition1 : there is no tags inside item tag) do ...
else  do ...

how can I write condition 1 ?

  • 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-23T16:06:03+00:00Added an answer on May 23, 2026 at 4:06 pm

    You can just test every child by iterating over the list of child nodes:

    public static boolean hasChildElements(Element el) {
        NodeList children = el.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
                return true;
            }
        }
        return false;
    }
    

    condition1 is then (! hasChildElements(el)).

    Alternatively, you can implement the test with getElementsByTagName("*").getLength() == 0. However, if there are sub elements, this method will traverse the whole fragment you’re testing, and allocate lots of memory.

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

Sidebar

Related Questions

i have parsed xml using dom parser,but when the xml element contains no value
I have a program which reads an XML file using the DOM functions: $doc
Give me an example on how to parse a custom xml file using DOM
I'm parsing an XML document into my own structure using DOM, but in another
I have a table that is dynamically built using DOM. It has 10 cols,
How can I remove this type p tag <p>&nbsp;</p> using DOM or regex? I
I have been using DOM for parsing my small xml docs for sometime.Afer reading
How to parse xml element using attribute value. i am using Dom for parsing
Hi BB developers I have a issue while parsing the xml data using DOM.
I'm using DOM to parse an xml file. And I am having trouble catching

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.