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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:24:26+00:00 2026-05-28T17:24:26+00:00

I would like to read an xml file. I’ ve found an example which

  • 0

I would like to read an xml file. I’ ve found an example which is good until the xml element doesn’t have any attributes. Of course i’ve tried to look after how could I read attributes, but it doesn’t works.

XML for example

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<car>
<properties>
<test h="1.12" w="4.2">
<colour>red</colour>
</test>
</properties>
</car>

Java Code:

public void readXML(String file) {
    try {

        File fXmlFile = new File(file);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

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

            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("test : "
                        + getTagValue("test", eElement));
                System.out.println("colour : " + getTagValue("colour", eElement));

            }
        }

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

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


    Node nValue = (Node) nlList.item(0);
            System.out.println(nValue.hasAttributes());

    if (sTag.startsWith("test")) {
        return eElement.getAttribute("w");

    } else {
        return nValue.getNodeValue();
    }

}

Output:

false

test :

false

colour : red

My problem is, that i can’t print out the attributes. How could i get the attributes?

  • 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-28T17:24:27+00:00Added an answer on May 28, 2026 at 5:24 pm

    There is alot wrong with your code; undeclared variables and a seemingly crazy algorithm. I rewrote it and it works:

    import java.io.File;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public final class LearninXmlDoc
    {
        private static String getTagValue(final Element element)
        {
            System.out.println(element.getTagName() + " has attributes: " + element.hasAttributes());
    
            if (element.getTagName().startsWith("test"))
            {
                return element.getAttribute("w");
    
            }
            else
            {
                return element.getNodeValue();
            }
        }
    
        public static void main(String[] args)
        {
            final String fileName = "c:\\tmp\\test\\domXml.xml";
    
            readXML(fileName);
        }
    
        private static void readXML(String fileName)
        {
            Document document;
            DocumentBuilder documentBuilder;
            DocumentBuilderFactory documentBuilderFactory;
            NodeList nodeList;
            File xmlInputFile;
    
            try
            {
                xmlInputFile = new File(fileName);
                documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilder = documentBuilderFactory.newDocumentBuilder();
                document = documentBuilder.parse(xmlInputFile);
                nodeList = document.getElementsByTagName("*");
    
                document.getDocumentElement().normalize();
    
                for (int index = 0; index < nodeList.getLength(); index++)
                {
                    Node node = nodeList.item(index);
                    if (node.getNodeType() == Node.ELEMENT_NODE)
                    {
                        Element element = (Element) node;
    
                        System.out.println("\tcolour : " + getTagValue(element));
                        System.out.println("\ttest : " + getTagValue(element));
                        System.out.println("-----");
                    }
                }
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a huge xml file (~ 85 Mo) and I would like
I have a javascript function which I try to read XML file. function readXML()
I would like to read/write encrypted XML files using LINQ to XML. Does anyone
I would like to read a website asynchronously, which isnt possible with urllib as
I would like to read a text file and input its contents into an
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
In java i would like to read a file line by line and print
I would like to read in a dynamic URL what contains a HTML file,
I have an XML document that I would like to update after it already

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.