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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:29:32+00:00 2026-05-31T14:29:32+00:00

I am trying to parse a xml file using dom parser. The xml fles

  • 0

I am trying to parse a xml file using dom parser. The xml fles contains lots of missing tags.
What i want to do is that , if i get any missing tag( like in example below 2nd artist tag do not contain image tag) i want to set String image as some value like “na”, so that i can put values in an array or a list.

Below is example

<list>
<artist>
<id>1</id>
<name>name1</name>
<image>name1.jpg</image>
</artist>
<artist>
<id>2</id>
<name>name2</name>
</artist>
</list>

My current code :

        NodeList list = element.getElementsByTagName("artist");
        if (list != null && list.getLength() > 0) {
            for (int i = 0; i < list.getLength(); i++) {

                Node item = list.item(i);

                NodeList properties = item.getChildNodes();

                for (int j = 0; j < properties.getLength(); j++) {

                    Node property = properties.item(j);

                    String name = property.getNodeName();

                    if (name.equalsIgnoreCase("__id")) {

                        try {

                            String strTitle = property.getFirstChild()
                                    .getNodeValue();
                            System.out.println(strTitle);

                        } catch (NullPointerException e) {
                            // TODO: handle exception

                            System.out.println("Null ID");
                        }

                    }

                    else if (name.equalsIgnoreCase("__name")) {

                        try {

                            String strTitle = property.getFirstChild()
                                    .getNodeValue();
                            System.out.println(strTitle);

                        } catch (NullPointerException e) {
                            // TODO: handle exception

                            System.out.println("Null Name");
                        }

                    }

                    else if (name.equalsIgnoreCase("__image")) {

                        try {

                            String strTitle = property.getFirstChild()
                                    .getNodeValue();
                            System.out.println(strTitle);

                        } catch (NullPointerException e) {
                            // TODO: handle exception

                            System.out.println("Null Image");
                        }

                    }
                }`
  • 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-31T14:29:34+00:00Added an answer on May 31, 2026 at 2:29 pm

    Add a String variable set to your default, assigned just before you enter the loop that parses child tags of artist. Then within your loop, if you find an image child, replace that String variable with the provided value.

    Edit: code based on the original:

            NodeList list = element.getElementsByTagName("artist");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
    
                    Node item = list.item(i);
    
                    NodeList properties = item.getChildNodes();
    
                    String strImage = "na"; // default
    
                    for (int j = 0; j < properties.getLength(); j++) {
    
                        Node property = properties.item(j);
    
                        String name = property.getNodeName();
    
                        if (name.equalsIgnoreCase("__id")) {
    
                            try {
    
                                String strTitle = property.getFirstChild()
                                        .getNodeValue();
                                System.out.println(strTitle);
    
                            } catch (NullPointerException e) {
                                // TODO: handle exception
    
                                System.out.println("Null ID");
                            }
    
                        }
    
                        else if (name.equalsIgnoreCase("__name")) {
    
                            try {
    
                                String strTitle = property.getFirstChild()
                                        .getNodeValue();
                                System.out.println(strTitle);
    
                            } catch (NullPointerException e) {
                                // TODO: handle exception
    
                                System.out.println("Null Name");
                            }
    
                        }
    
                        else if (name.equalsIgnoreCase("__image")) {
    
                            try {
    
                                String strTitle = property.getFirstChild()
                                        .getNodeValue();
                                                                strImage = strTitle;
                                System.out.println(strTitle);
    
                            } catch (NullPointerException e) {
                                // TODO: handle exception
    
                                System.out.println("Null Image");
                            }
    
                        }
                    }
                }
                System.out.println("Image: " + strImage);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse a xml file that i have (using javascript DOM).
I am trying to parse one XML file that contains some unicode characters.I tried
I'm trying to parse an XML file using PHP, but I get an error
I'm trying to parse a XML file using TBXML . However, this parser has
I am trying to parse an XML file using NSXMLParser, but the method [parser
I'm trying to parse an xml file with PHP. I'm using this code and
I have an XML file that I'm trying to parse with Linq-to-XML. One of
I am trying to parse an xml file using XmlReader but although I am
I'm trying to parse an xml file using lxml. xml.etree allowed me to simply
I am trying to parse an XML file using the SAX interface of libxml2

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.