This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Well, the XPath expression you're looking for is "BASE/ORDER/DETAILS/@category". However that isn't going to get you an MBElement, it's going to give you some other type which represents an attribute. If it works at all, that is. I googled and found the API docs for MBElement and I was astonished to find there was no way to access the element's attributes. Or at least no obvious (to me) way. Let me link this post to the Websphere forum, maybe somebody over there will have a better idea.
Joined: Jan 05, 2005
That was the notation I'd tried first . . but it didn't work.
However, I'm with you on the oddness of the API - no way to directly get the attribute I think . . but I know when you create an MbElement as a child of another, given the type parameter, you create an MbElement that is either a real child of the original, or it can be an attribute, or even a namespace, of the original.
It's weird. I guess they had their reasons for doing it that way. Hence why getFirstElementByPath("category") in the second line of code-fragment 2 actually works. It's an MbElement . . but not the same type of MbElement.
You may try the evaluateXPath method of MbElement. Use its overload with string expression for easy setup otherwise you can set up MbXPath first. Like this.
It might return Boolean, Double, String or List of MbElement according to the documentation. In the case where there are multiple BASE, ORDER, or DETAILS, it would probably return a List. In that case, use iterator to iterate through the returned list. Experimenting it a bit.
Joined: Jan 05, 2005
Paul Clapham wrote:Yuck. In XML an attribute is not an element. Neither is it a child of the element it belongs to.
But anyway you've got some code that works, no?
Yes I do - I don't like the code, but it works.
And, yes, I had some choice words less polite than "yuck" about non-elements being treated like elements . . . it's... unpleasant.
g tsuji: That looks good - I JUST started looking into the XPath part of the library, and may give that a shot - thanks!