为了账号安全,请及时绑定邮箱和手机立即绑定

XML 应该使用哪种 java 数据结构?

XML 应该使用哪种 java 数据结构?

汪汪一只猫 2022-11-02 16:56:43
我正在编写一个 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::goto'匹配的'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);我从 IDE 收到一条错误消息:“类型 Object 不是通用的;它不能用 arguments 参数化。
查看完整描述

1 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

你的问题有两个部分。

  1. 将 xml 文件/配置读入数据结构(jaxb 为您完成)。

    • 为此 xml 表示创建一个 xsd,

    • 使用 xjc 或 jaxb 生成 java 代码。

  2. 从 goto 获取引用的节点。

    • 将 xml 文件读取到 java 对象 Cyo。

    • 读取后,将命名节点存储在 hashmap Map map -> HashMap() 中。

    • 当您遍历goto时,Node gotoNode = map.get(theNodeName)

    • gotoNode.execute()


查看完整回答
反对 回复 2022-11-02
  • 1 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信