XPath (SimpleXML) 根据孩子兄弟姐妹的值选择孩子 [英] XPath (SimpleXML) selection of child based on value of child's sibling

查看:26
本文介绍了XPath (SimpleXML) 根据孩子兄弟姐妹的值选择孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SimpleXML (Java) 并且我正在尝试根据列表的兄弟姐妹之一的值获取对象列表.

I'm using SimpleXML (Java) and I'm trying to get a List of objects based on the value of one of the siblings of the list.

所以,这是我的 XML:

So, here's my XML:

<xml>
  <metadata>
    <resources>

      <resource>
        <ittype>Service_Links</ittype>
        <links>
          <link>
            <path>http://www.stackoverflow.com</path>
            <description>Stack Overflow</description>
          </link>
          <link>
            <path>http://www.google.com</path>
            <description>Google</description>
          </link>
        </links>
      </resource>

      <resource>
        <ittype>Article_Links</ittype>
        <links>
          ...
        </links>
      </resource>

      ...

    </resources>
  </metadata>
</xml>

我想要做的是使用 XPath 创建一个 SimpleXML 注释,以获得所有 "links" 的列表,其中列表的兄弟 "ittype"> 等于 Service_Links".

What I am trying to do is create a SimpleXML-annotation using XPath in order to get a List of all of the "links" where the list's sibling "ittype" equals "Service_Links".

因此,例如,这有效但仅当我尝试静态获取第一个 资源" 节点(而不是动态获取 资源">-node 基于它的兄弟 "ittype" 是:

So, for example, this works but only if I'm trying to statically get the 1st "resource"-node (as opposed to dynamically getting the "resource"-node based on what it's sibling "ittype" is:

@ElementList(name="links")
@Path("metadata/resources[1]/resource")
public List<Link> link;

我尝试了很多方法来格式化 xpath 以动态查找 "resource" 节点,我希望我无法在此处全部列出;但是,这是我尝试过的一个示例,在我看来它应该可以工作;不知道我做错了什么......

I've tried so many ways to format the xpath to dynamically find the "resource"-node I want that I couldn't possibly list them all here; however, this is one example I've tried that seems to me it should work; no clue what I'm doing wrong...

@ElementList(name="links")
@Path("metadata/resources[resource/ittype='Service_Links']/resource")
public List<Link> link;

我在谷歌上搜索了很多,包括这里的 SO,但没有找到非常相似的;而我所发现的,翻译得不够好,无法让我发现我需要做的事情.例如,这个 SO 没有即使非常相似,它似乎也能工作.

I've googled a ton, including here on SO, and just can't find much very similar; and what I did find, didn't translate well enough for me to spot what I need to do. For example, this SO didn't seem to work even though it's pretty similar.


更新 #1(80 月 8 日下午 12:38)

根据反馈,我已经尝试过:

Per feedback, I have tried this:

@ElementList
@Path("metadata/resources/resource[ittype='Service_Links']")
public List<Link> link;

没有骰子:org.simpleframework.xml.core.PathException: 字段 'links' 中路径 'metadata/resources/resource[ittype='Service_Links']' 的索引无效

No dice: org.simpleframework.xml.core.PathException: Invalid index for path 'metadata/resources/resource[ittype='Service_Links']' in field 'links'

推荐答案

我从来没有用 SimpleXml 来实现这种风格的 XPath;从更彻底地阅读他们的文档来看,他们似乎还没有实现这个 XPath 功能.

I never got SimpleXml to implement this style of XPath; from reading through their docs more thoroughly, it looks like they haven't implemented this XPath feature yet.

所以我在这里确定了不同的 XML 解析策略:https://stackoverflow.com/a/13236117/483257

So I settled on a different XML-parsing strategy here: https://stackoverflow.com/a/13236117/483257

这篇关于XPath (SimpleXML) 根据孩子兄弟姐妹的值选择孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆