user3510024 user3510024 - 3 months ago 27
Java Question

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.

AJJ AJJ
Answer

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

}
Comments