gpt4 book ai didi

java - 在复杂对象中查找对象

转载 作者:行者123 更新时间:2023-12-01 09:47:24 24 4
gpt4 key购买 nike

我正在使用 Justinmind 的 API 来开发插件。我需要的基本上是通过 ID 查找元素。但是,当我搜索整个文档时,该 API 似乎没有像 findById() 这样的方法 here 。我想我必须创建一个方法。在这里,我解释了 Justinmind 中的问题细节和总体结构,尽管问题不是 Justinmind 特有的

假设我有以下页面布局:

enter image description here

我将在 Justinmind 中设计此页面,如下所示:

Screen
----Image
----Group
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
----Text
----Text
----Text

在 Justinmind API 中,屏幕是一个 ICanvas 对象。我将屏幕存储在 myScreen 变量中。然后,getApiRoot() 返回页面的最顶层组件。之后,每个组件都是一个子组件,可以通过 getApiChildren() 方法访问。

List<IComponent> components = myScreen.getApiRoot().getApiChildren();

上面的代码返回图像、组和文本,即一阶层次结构组件。例如,我可以按如下方式访问第一个按钮的标签(OVERVIEW):

IComponent group = components.get(1); //Returns the Group
IComponent button1 = group.getApiChildren().get(0); //Returns the first button group, which is the first child of group
IComponent label1 = button1.getApiChildren().get(1); //Returns the label, which is the second child of the first button group

如您所见,我总是通过 getApiChildren() 方法访问组件。而这在大型动态页面中不太可行。例如,因为我正在编写的插件会在按钮组中缺少 a 标签时发出警报。为了检查标签是否存在,我必须使用 getApiChildren() 方法遍历屏幕的所有组件。

我可以通过树来表示它:

enter image description here

但是,这种结构总会发生变化。我应该构建一棵树还是一个 HashMap ?在非常复杂的对象中查找对象的最佳方法是什么?

最佳答案

我不知道justinmind。但是如果你有一个 Java 中的复杂对象图,你可以尝试这个库 http://commons.apache.org/proper/commons-jxpath/并使用 XPATH 语法来指定您的查询。

关于java - 在复杂对象中查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com