Skip to Content

Debugging DOM Parser in JAVA Mapping

Apr 13, 2017 at 01:52 PM


avatar image

Hi SDNites,

I am debugging JAVA mapping which is using DOM parser. I need some inputs in how to view the values of the variable while debugging like - input stream, Node values, DOM instance etc. When I try to open the variable it does not show the values like XML and also the node which I read etc.

Below is the code I wrote,

package pack_Dom_Parser;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Class_customer {

public static void main(String[] args) {

Node temp, node_fullname, text_node;
String fullName="";

try {
FileInputStream fis = new FileInputStream(
InputSource is = new InputSource(fis);

FileOutputStream fos = new FileOutputStream("C:/employee_output.xml");

/*creates new instance of parser*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//ignore whitespace within document
//parser is aware of namespace

// Creates DOM Document from input stream
Document document = factory.newDocumentBuilder().parse(is);
NodeList nl = document.getElementsByTagName("name");

for (int i = 0; i < nl.getLength(); i++) {
Node name_node = nl.item(i);
for (temp = name_node.getFirstChild(); temp != null; temp = name_node
.getNextSibling()) {
if (temp.getNodeName().equals("country")) {
if (temp.getFirstChild().getNodeValue().equalsIgnoreCase("India")) {
Node parent, child_name;
// Get parent node (name) of Node Country
parent = temp.getParentNode();

// Get first child Node (Firstname) of parent (name)
child_name = parent.getFirstChild();
if (child_name != null) {
fullName = child_name.getNodeValue() + " ";

//Get sibling of firstname which is lastname
child_name = child_name.getNextSibling();
if (child_name != null) {
fullName = fullName + child_name.getNodeValue();

// Create element fullname
node_fullname = document.createElement("fullname");
text_node = document.createTextNode(fullName);

//Append it to root element "employee"
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(fos));
} catch (Exception ex) {



10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Andrzej Filusz Apr 13, 2017 at 03:23 PM

Hi Abhi,

Well, a debugging of a such code (with a loop statement) can be difficult and time consuming (it depends on the number of iterations).

Sometimes the easiest way is to add lines like:


and then analyze the output. Of course, an analyzed variable must override toString() method.



10 |10000 characters needed characters left characters exceeded