user3510024
user3510024

Reputation: 3

Parsing XML file (java)

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.

Upvotes: 0

Views: 686

Answers (3)

sus007
sus007

Reputation: 295

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

Upvotes: 1

AJJ
AJJ

Reputation: 3628

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");
    }

}

Upvotes: 0

klarki
klarki

Reputation: 915

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.

Upvotes: 3

Related Questions