I have parsed a XML file using a DOM parser in Java but I require tips on how to actually use this information.
I have to create a method that takes the information parsed and sends out commands, based on certain information extracted from the file, to a software that I use at work, by using a socket.
The software I use maps communications. For example: since Federate Jurassic and Federate WWS communicate, one of the string commands would be used to map it.
I need assistance in beginning to write this method. Thanks.
This is my Java code so far:
public class Parse {
public static void main(String args[]) throws IOException, InterruptedException {
Socket s = null;
try {
s = new Socket("localhost", 8008);
System.out.println(s.isConnected());
File Configuration = new File("Configuration.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(Configuration);
doc.getDocumentElement().normalize();
System.out.println("root of xml file " + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("Federate");
NodeList n = doc.getElementsByTagName("Interface");
System.out.println("Federate names:");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
System.out.println(node.getAttributes().getNamedItem("name"));
}
System.out.println("");
System.out.println("Interface names:");
for (int i = 0; i < n.getLength(); i++) {
Node a = n.item(i);
System.out.println(a.getAttributes().getNamedItem("name"));
System.out.println(a.getAttributes().getNamedItem("publish"));
System.out.println(a.getAttributes().getNamedItem("subscribe"));
System.out.println("");
}
} catch (Exception ex) {
ex.printStackTrace();
}
String command1 = "{\"Command\":\"AddVertex\",\"Params\":{\"Attr\":{\"UniqueID\":\"Fed1\"},\"Show\":\"True\"}}";
String command2 = "{\"Command\":\"AddVertex\",\"Params\":{\"Attr\":{\"UniqueID\":\"Fed2\"},\"Show\":\"True\"}}";
String command3 = "{\"Command\":\"AddEdge\",\"Params\":{\"Attr\":{\"UniqueID\":\"30\"},\"Show\":\"True\",\"To\":\"Fed1\",\"From\":\"Fed2\"}}";
String command4 = "{\"Command\":\"ChangePreferences\",\"Params\":{\"Name\":\"Edge Colors\",\"Settings\":{\"Transformations\":{\"UniqueID\":{\"Method\":\"Enumerated\",\"Mapping\":[{\"Input\":\"30\",\"Output\":\"Green\"}]}},\"BasedOn\":\"UniqueID\"}}}";
String command5 = "{\"Command\":\"ReleaseEvents\"}";
String command6 = "{\"Command\":\"AddVertex\",\"Params\":{\"Attr\":{\"UniqueID\":\"Fed3\"},\"Show\":\"True\"}}";
String command7 = "{\"Command\":\"AddVertex\",\"Params\":{\"Attr\":{\"UniqueID\":\"Fed4\"},\"Show\":\"True\"}}";
String command8 = "{\"Command\":\"AddEdge\",\"Params\":{\"Attr\":{\"UniqueID\":\"30\"},\"Show\":\"True\",\"To\":\"Fed3\",\"From\":\"Fed2\"}}";
String command9 = "{\"Command\":\"AddEdge\",\"Params\":{\"Attr\":{\"UniqueID\":\"30\"},\"Show\":\"True\",\"To\":\"Fed4\",\"From\":\"Fed2\"}}";
String command10 = "{\"Command\":\"AddEdge\",\"Params\":{\"Attr\":{\"UniqueID\":\"30\"},\"Show\":\"True\",\"To\":\"Fed4\",\"From\":\"Fed3\"}}";
PrintWriter out = null;
out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
...
}
}
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<FederationConfiguration>
<Controller port="18909"/>
<ControllerTimeout value="1" units="ms"/>
<Federation name="JurassicWws">
<Federate name="WWS">
<StartupShutdownTimeout value="10" units="s"/>
<Interface name="Jurassic" instanceNumber="1" publish="false" subscribe="true"/>
</Federate>
<Federate name="Jurassic">
<StartupShutdownTimeout value="10" units="s"/>
<Interface name="Jurassic" instanceNumber="1" publish="true" subscribe="false"/>
<Interface name="Baz" instanceNumber="1" publish="true" subscribe="false"/>
</Federate>
<Federate name="FOO">
<StartupShutdownTimeout value="10" units="s"/>
<Interface name="Baz" instanceNumber="1" publish="false" subscribe="true"/>
<Interface name="Delta" instanceNumber="1" publish="true" subscribe="true"/>
</Federate>
<Federate name="BAR">
<StartupShutdownTimeout value="10" units="s"/>
<Interface name="Baz" instanceNumber="1" publish="false" subscribe="true"/>
<Interface name="Delta" instanceNumber="1" publish="true" subscribe="true"/>
</Federate>
</Federation>
</FederationConfiguration>