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.
The moose likes XML and Related Technologies and the fly likes MbElement - getting an attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "MbElement - getting an attribute "element" directly - Websphere Message Broker" Watch "MbElement - getting an attribute "element" directly - Websphere Message Broker" New topic
Forums: Websphere XML and Related Technologies
Author

MbElement - getting an attribute "element" directly - Websphere Message Broker

Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
Alright, I wasn't 100% sure that this question belonged here, or elsewhere, but here's what I'm running into:

I have code that needs to get the value of nodes, or the value of attributes, or possibly both.

So, let's say I have the following tree fragment of XML:


Now, I know I can easily get the value of the DETAILS node as follows, assuming xmlBody is an MbElement object representing the entire structure above:
code-fragment 1:


Therefore, value will be equal to "M5-100"

However, how do I get the value of the attribute "category"? The only solution I have so far is:
code-fragment 2:


Is there a way to indicate the attribute name within the original assignment of element? ie: is there a way that I can do:
code-fragment 3:


and thus have value be equal to "50"? Or do I HAVE to do the two-step process as I showed in code-fragment 2?

Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

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.
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

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?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 535
    
    3
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.
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
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?


Paul,

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MbElement - getting an attribute "element" directly - Websphere Message Broker