2

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.

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>

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".

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;

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;

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.



UPDATE #1 (8OCT @ 12:38pm)

Per feedback, I have tried this:

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

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

Community
  • 1
  • 1
Bane
  • 1,772
  • 3
  • 21
  • 28

3 Answers3

1

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.

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

Community
  • 1
  • 1
Bane
  • 1,772
  • 3
  • 21
  • 28
0

I don't know about SimpleXML specifically, but this xpath should get you the elements you want:

metadata/resources/resource[ittype = 'Service_Links']
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
0
  1. Your xml example is not well-formed. The link element doesn't have its closing counterpart. The same applies for the description el. I assume these are just typos.
  2. Here is the working XPath expression:

/metadata/resources/resource/links/link[parent::links/preceding-sibling::ittype/text()=='Service_Links']

or much simpler version:

/metadata/resources/resource[ittype = 'Service_Links']/links/link

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • Thank you for the reply; yes, those were just typos in my post here on SO; I've corrected them; will try your answer today and provide feedback. – Bane Oct 08 '12 at 15:42
  • @Jiri... FYI... the XPath you provided doesn't work exactly like that using SimpleXML (or at least I don't think it does; I've tried it, didn't seem to); closest thing is how Ian's answer is formatted though that also doesn't work (see the "update" at the bottom of my question). – Bane Oct 08 '12 at 21:25
  • Sorry, I don't know the SimpleXML framework, but the XPath itself should be ok (http://www.xpathtester.com). What about escaping the quotes? here is similar thread with similar exception http://stackoverflow.com/questions/12298673/how-do-i-use-the-value-of-an-xml-element-when-setting-the-path-in-the-java-simpl – Jiri Kremser Oct 08 '12 at 22:54
  • @Jiri... yeah, no you're xpath is right I'm sure, just doesn't seem to fit SimpleXml's API (or I can't seem to get it to fit anyways); I appreciate the link to xpathtester though, thanks! That'll come in handy. – Bane Oct 08 '12 at 23:01