This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes How can I create fully qualified names using a sax DefaultHandler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How can I create fully qualified names using a sax DefaultHandler" Watch "How can I create fully qualified names using a sax DefaultHandler" New topic
Author

How can I create fully qualified names using a sax DefaultHandler

Max Tomlinson
Ranch Hand

Joined: Jul 17, 2001
Posts: 365
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

"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

Joined: Jul 17, 2001
Posts: 365
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jul 17, 2001
Posts: 365
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jul 17, 2001
Posts: 365
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
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
Consider Paul's rocket mass heater.
 
subject: How can I create fully qualified names using a sax DefaultHandler