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 java.io.*;
import java.io.FileInputStream;
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 javax.xml.transform.stream.StreamResult;
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(
"C:/employee_input.xml");
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
factory.setIgnoringElementContentWhitespace(true);
//parser is aware of namespace
factory.setNamespaceAware(true);
// 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);
node_fullname.appendChild(text_node);
//Append it to root element "employee"
name_node.getParentNode().appendChild(node_fullname);
}
}
}
}
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(fos));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Abhi
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:
System.out.println(name_of_variable);
and then analyze the output. Of course, an analyzed variable must override toString() method.
Regards,
Andrzej