-1

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>
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • What code do you have so far? – Justin Jasmann May 09 '14 at 18:10
  • You are asking a general and vague question. Obviously, other people don't know what information your XML file contains and how this translates to the commands you're talking about, so it's going to be hard to help you with this. – Jesper May 09 '14 at 18:10

1 Answers1

1

you need to have a structure for your code, use simple OOP...

your xml parser

Class MyXmlParser{

public ArrayList<Commands> parse(String xmlFileName){
} 

public ArrayList<Commands> parse(String xmlString){
} 
}

and a command class

Class Command {
private String command = "";
private String options = "";
:
:
//setters 

//getters

}

command sender class..

Class CommandSender{
private Socket socket = null;

public CommandSender(String IP, int Port){
//init socket here ...
}

public boolean sendCommand(Command cmd){
//send command and return true/false...
}

public void close(){
//close the socket...
}
}

finally ur main class

Class AppStarter{
public Static Void Main(String args[]){
MyXmlParser xmlParser = new MyXmlParser();
ArrayList<Commands> cmds = xmlParser.parse(args[0]);
CommandSender cmdSender = new CommandSender("192.168.1.110",8081);

for(a=0;....cmds.size...){
cmdSender.sendCommand(cmds.get(a));
}//for loop

}//main()

}
Yazan
  • 6,074
  • 1
  • 19
  • 33