0

I have xml file like this

  <Sample_Data>
    <lastname Name="lastname" Value="FIRSTNAME" />
    <firstname Name="firstname" Value="LASTNAME" />
    <DM Name="DM" Value="93.2" />
    <CP Name="CP" Value="7.7" />
  </Sample_Data>

I need to read this file with java and I use code like this:

 try {

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse("File.xml");

    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("Sample_Data");

    System.out.println("----------------------------");

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

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;


            System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
            System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
            System.out.println("DM : " + eElement.getElementsByTagName("DM").item(0).getTextContent());
            System.out.println("CP : " + eElement.getElementsByTagName("CP").item(0).getTextContent());

        }
    }

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

But I can't get values of firstname, lastname, DM and CP I get empty values for them.

Can someone help me to resolve this problem?

2 Answers 2

2

All your elements doesn't have a text node. So getTextContent() is empty. You should read the attributes instead.

getElementsByTagName().item(0) returns a Node, so you should first cast it to Element.

((Element) eElement.getElementsByTagName("firstname").item(0)).getAttribute("Value")

In procuction code, you should also check, if the NodeList eElement.getElementsByTagName("firstname") contains some items.

Sign up to request clarification or add additional context in comments.

Comments

0

I would say that the best thing to do is to get a proper XML file. That way you would not have to parse multiple attributes.

You got that :

<Sample_Data>
    <lastname Name="lastname" Value="FIRSTNAME" />
    <firstname Name="firstname" Value="LASTNAME" />
    <DM Name="DM" Value="93.2" />
    <CP Name="CP" Value="7.7" />
</Sample_Data>

Instead you should have something like that :

<Sample_Data>
    <lastname> FIRSTNAME </lastname>
    <firstname> firstname </firstname>
    <DM> 93.2 <DM/>
    <CP> 7.7 </CP>
</Sample_Data>

Wouldn't that be easier to work with ?

1 Comment

Yes that would be easier but I get xml file like in my example. Thanks for your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.