我正在编写一个Java程序,该程序使用XML文件作为模板来填充数据结构,然后该程序接受用户输入并与之交互。
我要追求的是保持这种层次结构的东西:
<CYO>
<node>
<to>
home
</to>
<message>
This is the message that will be displayed to the user.
</message>
<option>
<name>
fun <!-- option that is dispalyed and command to be entered -->
</name>
<goto>
not home <!-- next node to be displayed for this command -->
</goto>
</option>
<option>
<name>
no fun
</name>
<goto>
home <!-- should trigger the reprinting of the same message; goto/home = to/home -->
</goto>
</option>
</node>
<node>
<to>
not home
</to>
<message>
Welcome you have just left home.
What else do you want to do?
</message>
<option>
<name>
back home
</name>
<goto>
home
</goto>
</option>
<option>
<name>
fun
</name>
<goto>
not home <!-- should trigger the reprinting of the same message; goto/not home = to/not home -->
</goto>
</option>
</node>
</CYO>
在Java中,我一直试图对如何从'option / name :: goto'调用节点项的逻辑进行排序,然后让程序返回与'option / name'相匹配的'node / to' ::去'。真正的问题是我不确定如何用Java表示它,这样的调用才能正确返回。到目前为止,这是我所做的Java:
List<Map<String, Object<String, String>>> CYO = new ArrayList();
Map<String, Object> home = new HashMap<String, Object>();
home.put("to", "home");
home.put("message", "This is the message that will be displayed to the user.");
Map<String, String> Option1 = new HashMap<String, String>();
Option1.put("fun", "not home");
Option1.put("no fun", "home");
home.put("option", Option1);
Map<String, Object> not_home = new HashMap<String, Object>();
not_home.put("to", "not home");
not_home.put("message", "Welcome you have just left home.\\nWhat else do you want to do?");
not_home.put("option", "not home");
Map<String, String> Option2 = new HashMap<String, String>();
Option2.put("back home", "home");
Option2.put("fun", "not_home");
not_home.put("option", Option2);
CYO.add(home);
CYO.add(not_home);
System.out.println(CYO.indexOf("not_home"));
我从IDE中收到一条错误消息:“对象类型不是通用的;不能使用参数对其进行参数化。
答案 0 :(得分:0)
您的问题有两个部分。
将xml文件/配置读入数据结构(jaxb为您完成)。
从goto获取引用的节点。
遍历到 goto 时,节点gotoNode = map.get(theNodeName)
gotoNode.execute()