0

I have this XML file(Param.xml):

<?xml version="1.0" encoding="UTF-8"?>

<param-config>

    <parameter>
        <tag> BNsprate  </tag>
        <value> 0.8 </value>
    </parameter>

    <parameter>
        <tag> CellId_nbr  </tag>
        <value> 1 </value>
    </parameter>

    <parameter>
        <tag> Calls_nbr  </tag>
        <value> 2 </value>
    </parameter>

    <parameter>
        <tag> Call_time  </tag>
        <value> 00:02:00 </value>
    </parameter>

    <parameter>
        <tag> InCalls_nbr  </tag>
        <value> 0 </value>
    </parameter>

    <parameter>
        <tag> Sms_nbr  </tag>
        <value> 0 </value>
    </parameter>


</param-config>

I wrote this java code to print this file content :

  private String paramReader(String tag) {

    String value = "";
    try {

        InputStream is = this.getClass().getClassLoader()
                .getResourceAsStream("Param.xml");

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);

        doc.getDocumentElement().normalize();

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

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

            Node nNode = nList.item(i);
            System.out.println(nNode.getNodeValue());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                if (eElement.getElementsByTagName("tag").item(0)
                        .getTextContent() == tag) {

                    value = eElement.getElementsByTagName("value").item(0)
                            .getTextContent();
                }
            }
        }
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;

}

This returns : null null null null null null Please, I'm asking why it just reads the file whithout getting content values? Thanks.

3 Answers 3

3

You're comparing String to a String using equality sign, while you should use .equals method here:

if(eElement.getElementsByTagName("tag").item(0).getTextContent()==tag){

instead it should be like this:

if(eElement.getElementsByTagName("tag").item(0).getTextContent().equals(tag)){

== sign checks equality of the reference, i.e. returns true if both reference the same object in memory, while you want to check the equality of value, which is what .equals() method does.

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

1 Comment

Thank you. But it dosen't resolve the problem, the problem is that I can't print different values of "nNode". "nList" is empty that's exactly the problem. Because it contains 6 null elements.
1

For XML parsing and back again you should have to use XStream API. It is very easy to manipulate.Check this link

http://x-stream.github.io/tutorial.html

Comments

0

Modified version is below. This will fetch the text content of value node of input tag node.

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;

/**
 * @author JayaPrasad
 * 
 */
public class ParseXml {

    private String paramReader(String tag) {

        String value = "";
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse("parse.xml");

            doc.getDocumentElement().normalize();

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

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

                Node nNode = nList.item(i);
//              NodeList childNodes = nNode.getChildNodes();
//              for (int j = 0, length = childNodes.getLength(); j < length; j++) {
                // System.out.println(childNodes.item(j).getNodeName() + ":"
//                   + childNodes.item(j).getTextContent());
//                  if (childNodes.item(j).getNodeName().equals(tag)) {
//                      value = childNodes.item(j).getTextContent();
//                       System.out.println("Tag Node Value ::: " + value);
//                  }
//              }

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = null;
                    if (nNode instanceof Element) { // Safest way before casting
                        eElement = (Element) nNode;
                    }

                    if (eElement.getElementsByTagName("tag").item(0)
                            .getTextContent().trim().equals(tag)) {

                        value = eElement.getElementsByTagName("value").item(0)
                                .getTextContent();
                        System.out.println("Result value :: " + value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;

    }

    public static void main(String[] args) {
        ParseXml xmlObj = new ParseXml();
        xmlObj.paramReader("Call_time");
    }

}

4 Comments

@user3510024 wat is the actual problem? Which node value you require?
I want exactly to print "value", but the problem is that it prints nothing(I mean that "value" is null. What do you think?
@user3510024 i have modified your code a little. The program reads the file with content value.
Thank you so much, every thing is ok.

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.