我在Java中使用DOM解析器解析了一个XML文件但我需要有关如何实际使用这些信息的提示。
我必须使用套接字创建一个方法,该方法将解析的信息和基于从文件中提取的某些信息发送到我在工作中使用的软件的命令。
我使用的软件映射通信。例如:由于 Federate Jurassic 和 Federate WWS 进行通信,因此将使用其中一个字符串命令对其进行映射。
我开始编写此方法时需要帮助。感谢。
到目前为止,这是我的Java代码:
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文件:
<?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>
答案 0 :(得分:1)
你需要有一个代码结构, 使用简单的OOP ......
你的xml解析器
Class MyXmlParser{
public ArrayList<Commands> parse(String xmlFileName){
}
public ArrayList<Commands> parse(String xmlString){
}
}
和命令类
Class Command {
private String command = "";
private String options = "";
:
:
//setters
//getters
}
命令发送者类..
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...
}
}
最后是你的主要课程
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()
}