• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I create fully qualified names using a sax DefaultHandler

 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All-

I am using a DefaultHandler to parse a response file.

I want to be able to build fully qualified names, not just using the leaf node in EndElement. This is because of duplication in leaf node names and I need to determine the actual path so I need a unique name. Does anyone have a snippet or suggestion for doing this?

Many thanks
Max
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Fully qualified name" just means an element name with a namespace prefix and a namespace URI to go with it. It's easy enough to make startElement and endElement produce one of those, you just have to fill in the parameters properly.

But your question doesn't seem to be related to namespaces in any way. Duplication in leaf node names? XML documents are allowed to have many elements with the same name. Actual path? What path is that? I think we need more information about your problem.
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using a proprietary xml query tool at the co where I work and am getting elements back such as:
b.Number when I look for specific tags. I want tags such as foo/bar/Number - the fully qualified path name.
I want to build the path before I get to the leaf node so I can distinguish multiple nodes that end in 'Number'.

Using a sax handler I am not familiar with how to do this and wondered if it was a common enough routine that someone might have a sample.

thanks!
max
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that just mean that you want to generate an XPath expression which identifies a particular element? That's easy enough to do in SAX, you just keep a stack of the element names and maintain it in the startElement and endElement methods. If you want an XPath expression which identifies which of the various elements named Number you're looking at, then use a map whose keys are element name and whose values are sequence numbers. Then from those two things you can generate an XPath expression like /foo/bar[2]/Number[42].
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul-

"That's easy enough to do in SAX, you just keep a stack of the element names and maintain it in the startElement and endElement methods."

This will work for me. How can I determine a leaf node and beginning of the element path using DefaultHandler?

thank you
Max
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, a leaf node is one which doesn't have any children. (Perhaps you weren't including text node children in your definition of "leaf node" but that's just an input to how you do your programming.) And you won't know that until the parser reaches the end of the element, so therefore it's the endElement method which would do the work. Of course the startElement method would have to set some "number of children" variable to zero, and also increment its parent "number of children" -- this would be just below the top of the stack. And you might have to keep track of text children, to see if there are any.

(Or you could use a "has children" boolean variable instead.)

As for the second question, I don't understand what you mean by "the beginning of the element path". Unfortunately your questions so far have had a lot of non-standard terminology, it might help if you studied up a little bit more on XML as it's much easier if we all use the same names for things.
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's pretty clear what I want to do:

I want a fully qualified path name: foo/bar/etc

I DO NOT want just 'etc'.

Does anyone else have a code snippet that does this with a DefaultHandler?

thanks
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want a fully qualified path name: foo/bar/etc



Max, your interpretation of what "fully qualified" means is not correct.

Qualified means that a namespace and the element name are included in an element's start-tag and end-tag. Below is an
example. "citigroup" is the name of the namespace. "transaction" is the element name. Both of them in the start-tag and end-tag is fully qualified.



The element name in the start-tag below is not fully qualified.




Aside, SAX programming is a bit more difficult that a classroom "Hello Word" assignment. What you are seeking to do is relatively easy once you gain some knowledge of how SAX-compliant parsers actually work and how to write code that communicates with the parser's callbacks.

As mentioned, it might help if you studied up a little bit more on SAX and XML. Providing you with code would be hurting you rather than helping you. Good luck!
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Max Tomlinson wrote:I think it's pretty clear what I want to do:

I want a fully qualified path name: foo/bar/etc

I DO NOT want just 'etc'.

Does anyone else have a code snippet that does this with a DefaultHandler?

thanks



Well, I didn't find it all that clear. I thought I understood it but then you threw "beginning of the element path" out and that just confused me.

And now two people have told you what "fully qualified name" means in XML, so please don't persist in using that term to describe your requirements. And as for the code snippet, read this: NotACodeMill. If you want to generate an XPath expression like that, then you'll need an algorithm which keeps track of the current node and its ancestors in a stack, as already suggested.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic