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

The Archive Base Latest Questions

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

I have this XML document: <?xml version=1.0 encoding=utf-8?> <RootElement> <Achild> ….. </Achild> </RootElement> How

  • 0

I have this XML document:

<?xml version="1.0" encoding="utf-8"?>
<RootElement>
   <Achild>
      .....
   </Achild>
</RootElement>

How can I check if the document contains Achild element or not? I tried

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Use the factory to create a builder
try {
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.parse(configFile);
    final Node parentNode = doc.getDocumentElement();
    final Element childElement = (Element) parentNode.getFirstChild();
    if(childElement.getNodeName().equalsIgnoreCase(...

but it gives me an error (childElement is null).

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

    I think that you’re getting #text node (that between <RootElement> and <Achild>) as first child (that’s pretty common mistake), for example:

    final Node parentNode = doc.getDocumentElement();
    Node childElement = parentNode.getFirstChild();
    System.out.println(childElement.getNodeName());
    

    Returns:

    #text
    

    Use instead:

    final Node parentNode = doc.getDocumentElement();
    NodeList childElements = parentNode.getChildNodes();
    for (int i = 0; i < childElements.getLength(); ++i)
    {
        Node childElement = childElements.item(i);
        if (childElement instanceof Element)
            System.out.println(childElement.getNodeName());
    }
    

    Wanted result:

    Achild
    

    EDIT:

    There is second way using DocumentBuilderFactory.setIgnoringElementContentWhitespace method:

    factory.setIgnoringElementContentWhitespace(true);
    

    However this works only in validating mode, so you need to provide DTD in your XML document:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE RootElement [
        <!ELEMENT RootElement (Achild)+>
        <!ELEMENT Achild (#PCDATA)>
    ]>
    <RootElement>
       <Achild>some text</Achild>
    </RootElement>
    

    and set factory.setValidating(true). Full example:

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setIgnoringElementContentWhitespace(true);
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.parse("input.xml");
    
    final Node rootNode = doc.getDocumentElement();
    final Element childElement = (Element) rootNode.getFirstChild();
    
    System.out.println(childElement.getNodeName());
    

    Wanted result with original code:

    Achild
    
    • 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 like this: <?xml version=1.0 encoding=utf-8?> <RootElement> <Achild> ..... </Achild>
I have a XML document that looks something like this: <?xml version=1.0 encoding=UTF-8?> <citizen>
Lets assume we have this xml: <?xml version=1.0 encoding=UTF-8?> <tns:RegistryResponse status=urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure xmlns:tns=urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0 xmlns:rim=urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0> <tns:RegistryErrorList
I have only this in my mxml source code: <?xml version=1.0 encoding=utf-8?> <mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml
I have an xml document: <?xml version=1.0 encoding=utf-8?> <Root> <Child name=MyType compareMode=EQ></Child> </Root> And
I have the following XML document: <?xml version=1.0 encoding=UTF-8?> <Offices id=0 enabled=false> <office />
I have the following XML document <?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009' gd:etag='W/DEAERH47eCl7ImA9WhZTFEQ.'><id>http://code.google.com/feeds/issues/p/chromium/issues/full/921</id><published>2008-09-03T22:51:22.000Z</published><updated>2011-03-19T01:05:05.000Z</updated><title>Incorrect rendering</title><content
I have the following JSF table: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have the following Xml in my Resources.xmltest: <?xml version=1.0 encoding=utf-8 ?> <Response xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
I am writing an XML document in C#. I have something like this... string

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.