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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:19:34+00:00 2026-06-18T09:19:34+00:00

I tried a lot parsing the following XML using DOM. I am not able

  • 0

I tried a lot parsing the following XML using DOM. I am not able to print the values of elements. Can anybody help me to print the values of the elements.

Thanks in advance

<tns:CustomerDetails xmlns:xe="http://www.w3.org/2001/04/xmlenc#" xmlns:xd="http://www.w3.org/2000/09/xmldsig#" xmlns:tns="http://abc.com/elements" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pf="http://abc.com/Service">
  <tns:CustomerId>360817</tns:CustomerId>
  <tns:CustomerName>ABC Corp</tns:CustomerName>
  <tns:PAN>awap</tns:PAN>
  <tns:Timestamp>2010-05-20T12:20:19Z</tns:Timestamp>
  <tns:RequestId>397</tns:RequestId>
  <tns:PIN>1234</tns:PIN>
</tns:CustomerDetails>

My code

      File infile = new File("D:\\Cust.xml");
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse(infile);
  doc.getDocumentElement().normalize();

  System.out.println("root of xml file " +
                     doc.getDocumentElement().getNodeName());
  System.out.println("==========================");

  NodeList list = doc.getElementsByTagName("CustomerDetails");
  System.out.println(list.getLength());
  Element name = doc.getElementById("CustomerId");
  if (name == null) {
    System.out.println("There is no element with the ID ");
  } else {
    Text text = (Text)name.getFirstChild();
    System.out.println("The ID " + " locates the name " + text.getData());
  }

I have tried with

Element name = doc.getElementById("tns:CustomerId");

also..
i get null while printing

  • 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-18T09:19:35+00:00Added an answer on June 18, 2026 at 9:19 am

    You’re mixing your models here. getElementById is for elements that have an attribute identified by the document’s DTD as an ID, and since your document doesn’t have a DTD it will never give you anything useful.

    Because your document uses namespaces you should use the “NS” methods to extract elements, and for the text contained in an element you can use getTextContent

      File infile = new File("D:\\Cust.xml");
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      // namespaces - DocumentBuilderFactory is *not* namespace aware by default
      dbFactory.setNamespaceAware(true);
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(infile);
      doc.getDocumentElement().normalize();
    
      System.out.println("root of xml file " +
                         doc.getDocumentElement().getNodeName());
      System.out.println("==========================");
    
      NodeList list = doc.getElementsByTagNameNS(
            "http://abc.com/elements", "CustomerDetails");
      System.out.println(list.getLength());
      for(int i = 0; i < list.getLength(); i++) {
        Element custDetails = (Element)list.item(i);
        Element id = custDetails.getElementsByTagNameNS(
            "http://abc.com/elements", "CustomerId").item(0);
        System.out.println("Customer ID: " + id.getTextContent());
        Element name = custDetails.getElementsByTagNameNS(
            "http://abc.com/elements", "CustomerName").item(0);
        System.out.println("Customer Name: " + name.getTextContent());
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried a lot of the solutions in stackoverflow but I'm not able to
I have tried a lot but not been able to understand the steps involved
My layout sometimes does not position elements properly. I tried a lot of solutions
I have tried a lot of solutions but it did not work for me
I searched and tried a lot, but I can't accomplish it as I want..
I've come here for help with a complicated query. I've tried a lot of
I can't get expand_aliases to take effect in bash. I've tried a lot of
I have a strange behavior when parsing a large xml (but I'm not sure
I need help parsing out some text from a page with lxml. I tried
I tried a lot of things, first in the css now with jquery but

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.