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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:47:37+00:00 2026-06-02T17:47:37+00:00

I need to read a XML file with java. File structure as below. <?xml

  • 0

I need to read a XML file with java. File structure as below.

<?xml version="1.0" encoding="UTF-8"?>
<xml_tool xmlns:md="http://www.example.com/XT/1.0/">
    <md:header>
        <md:application_version>1.0</md:application_version>
        <md:export_date>19-04-2012</md:export_date>
        <md:export_time>14:55</md:export_time>
        <md:export_user>USER01</md:export_user>
    </md:header>
    <md:table table_name="CUSTOMER" key="customer number" record_count="2" column_count="5">
        <md:record>
            <md:column name="customer_number">123456</md:column>
            <md:column name="reg_date">01-04-2012</md:column>
            <md:column name="customer_name">Test Customer</md:column>
            <md:column name="customer_type">Normal </md:column>
            <md:column name="comments">This is a test record</md:column>
        </md:record>
        <md:record>
            <md:column name="customer_number">555111</md:column>
            <md:column name="reg_date">02-04-2012</md:column>
            <md:column name="customer_name">Test Customer</md:column>
            <md:column name="customer_type">VIP </md:column>
            <md:column name="comments">This is a test record</md:column>
        </md:record>
    </md:table>
</xml_tool>

I have read How to read XML file in Java – (DOM Parser) example and try to do my work. But I’m unable to read XML file successfully.

My Code

    try {

        File fXmlFile = new File("c:\\file.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("md:record");

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

            Node node = nList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) node;

                System.out.println(getTagValue("md:column", eElement));


            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

            Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
      }

and Result is

123456
555111

How could I read this XML file?

  • 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-06-02T17:47:38+00:00Added an answer on June 2, 2026 at 5:47 pm

    Update your for loop…

    for (int i = 0; i < nList.getLength(); i++)
    {
          Node node = nList.item(i);
          if (node.getNodeType() == Node.ELEMENT_NODE)
          {                 
                 if(eElement.hasChildNodes())
                 {
                     NodeList nl = node.getChildNodes();
                     for(int j=0; j<nl.getLength(); j++)
                     {
                         Node nd = nl.item(j);
                         System.out.println(nd.getTextContent());
                     }
                 }
           }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read an XML file using Java. Its contents are something like
i need to read an external XML file from my java application in jar
I need to simply read an .xml file; like this: <exchanges> <exchange> <timestamp>2010-08-19 17:15:56</timestamp>
I need to read smallish (few MB at the most, UTF-8 encoded) XML files,
I need to retrieve some details from a MusicXML (xml) file in Java. I
I read the next answer about load file into java application . I need
I want to write a java code to read the input XML file and
I need to read a XML file returned from a web service. I was
Hello I've written small class in java to read in xml file as a
I have a package with multiple java classes inside. I need to read xml

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.