File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 650
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic