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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:06:06+00:00 2026-05-17T19:06:06+00:00

I have a question concerning XML, Java’s use of DOM, and empty nodes. I

  • 0

I have a question concerning XML, Java’s use of DOM, and empty nodes. I am currently working on a project wherein I take an XML descriptor file of abstract machines (for text parsing) and parse a series of input strings with them. The actual building and interpretation of these abstract machines is all done and working fine, but I have come across a rather interesting XML requirement. Specifically, I need to be able to turn an empty InputString node into an empty string (“”) and still execute my parsing routines. The problem, however, occurs when I attempt to extract this blank node from my XML tree. This causes a null pointer exception and then generally bad things start happening. Here is the offending snippet of XML (Note the first element is empty):

    <InputStringList>
        <InputString></InputString>
        <InputString>000</InputString>
        <InputString>111</InputString>
        <InputString>01001</InputString>
        <InputString>1011011</InputString>
        <InputString>1011000</InputString>
        <InputString>01010</InputString>
        <InputString>1010101110</InputString>
    </InputStringList>

I extract my strings from the list using:

//Get input strings to be validated
xmlElement = (Element)xmlMachine.getElementsByTagName(XML_INPUT_STRING_LIST).item(0);
xmlNodeList = xmlElement.getElementsByTagName(XML_INPUT_STRING);
for (int j = 0; j < xmlNodeList.getLength(); j++) {

    //Add input string to list
    if (xmlNodeList.item(j).getFirstChild().getNodeValue() != null) {
        arrInputStrings.add(xmlNodeList.item(j).getFirstChild().getNodeValue());

    } else {
        arrInputStrings.add("");

    }
}

How should I handle this empty case? I have found a lot of information on removing blank text nodes, but I still actually have to parse the blank nodes as empty strings. Ideally, I would like to avoid using a special character to denote a blank string.

Thank you in advance for your time.

  • 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-17T19:06:07+00:00Added an answer on May 17, 2026 at 7:06 pm
    if (xmlNodeList.item(j).getFirstChild().getNodeValue() != null) {
    

    nodeValue shouldn’t be null; it would be firstChild itself that might be null and should be checked for:

    Node firstChild= xmlNodeList.item(j).getFirstChild();
    arrInputStrings.add(firstChild==null? "" : firstChild.getNodeValue());
    

    However note that this is still sensitive to the content being only one text node. If you had an element with another element in, or some text and a CDATA section, just getting the value of the first child isn’t enough to read the whole text.

    What you really want is the textContent property from DOM Level 3 Core, which will give you all the text inside the element, however contained.

    arrInputStrings.add(xmlNodeList.item(j).getTextContent());
    

    This is available in Java 1.5 onwards.

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

Sidebar

Related Questions

I have question concerning a function I created. I would like to show the
I have a question concerning active record association, referring to this part of the
I have a question concerning subtypes of built-in types and their constructors. I want
I have a question concerning Core Data and how, if at all, Entities get
I have a question concerning number conversion i JS. I have a number like
I have a simple question concerning Javascript. I am trying to print in a
I have a strange question concerning subroutines: As I'm creating a minimal language and
I have a really basic question concerning unix timestamp and mysql date. I'm trying
I have a question about the singleton pattern. I saw two cases concerning the
My question is concerning an application that will use a host application where the

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.