If condition not matched in DOM XLM parsing activity



I am a newbie in XML JAVA parsing using DOM and have built a small parser to read an application XML file with more or less standards structure.


My objective is to filter the results based on the value of a specific element called "deploymentStatus": if the element value is set to "Success", then I would still print the child nodes and attributes, but if it any other value than "Success" then I would just print out something like "Application undeployed" and not read the childs.


Currently I am able to get all applications names, however in the final result I lose the other nodes and child values (deployment Status, ServiceInstance, machine, status). Parser code below:



*import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Hrb_auto {

public static void main(String[] args) {
// TODO Auto-generated method stub
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document= builder.parse(new File ("C:\\Temp\\hrb_check \\AppStatus.xml"));

final Element racine = document.getDocumentElement();


final NodeList racineNoeuds = racine.getChildNodes();
final int nbRacineNoeuds = racineNoeuds.getLength();

for (int i = 0; i<nbRacineNoeuds; i++) {
if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
final Element application = (Element) racineNoeuds.item(i);

System.out.println("\n Application name: " + application.getAttribute("name"));

final NodeList AppNoeuds = application.getChildNodes();
final int nbAppNoeuds = AppNoeuds.getLength();

for (int j = 0; j<nbAppNoeuds; j++) {
if (AppNoeuds.item(j).getNodeType() == Node.ELEMENT_NODE) {
final Element service = (Element) AppNoeuds.item(j);
if (service.hasAttribute("name")) {
System.out.print("\t Service name: " + service.getAttribute("name"));

final Element deploymentStatus = (Element) service.getElementsByTagName("deploymentStatus").item(0);
final Element serviceInstance = (Element) service.getElementsByTagName("serviceInstance").item(0);


if (deploymentStatus.getTextContent() == "Success") {
System.out.print("\t deploymentStatus: " + deploymentStatus.getTextContent());
System.out.print("\t Service Instance: " + serviceInstance.getAttribute("name"));

final Element machine = (Element) serviceInstance.getElementsByTagName("machine").item(0);
final Element status = (Element) serviceInstance.getElementsByTagName("status").item(0);

System.out.print("\t machine: " + machine.getTextContent());
System.out.println("\t status: " + status.getTextContent());

} else {
System.out.println("Service disabled, no service to be checked");
}

}
else {
System.out.println("Application undeployed, no service to be checked");
}

}

}
}


}

}

catch (final ParserConfigurationException e) {
e.printStackTrace();
}
catch (final SAXException e) {
e.printStackTrace();
}
catch (final IOException e) {
e.printStackTrace();
}

}


}


Result obtained:



*Application name: CustomerApplications/TEST3/TEST
Service name: INFileAdapterConfiguration.aarService disabled, no service to be checked
Service name: RTCISOutFileAdapterConfiguration.aarService disabled, no service to be checked*


Expected result:



*Application name: CustomerApplications/TEST3/TEST
Service name: INFileAdapterConfiguration.aar deploymentStatus: Success Service Instance: TEST3 machine: test status: Unknown. HawkAgent on test is not responding
Service name: OutFileAdapterConfiguration.aar deploymentStatus: Success Service Instance: TEST3 machine: test status: Unknown. HawkAgent on test is not responding*


Any help provided would be great!


Thanks in advance


David


No comments:

Post a Comment