Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a virtual node structure for unavailable nodes. #3

Open
mathieucarbou opened this issue Jul 29, 2013 · 0 comments
Open

Provide a virtual node structure for unavailable nodes. #3

mathieucarbou opened this issue Jul 29, 2013 · 0 comments

Comments

@mathieucarbou
Copy link
Owner

Reported by christopher.klewes, Apr 20, 2011
First I would like to say thanks for your framework. It's really nice and I'm loving to create XML documents with it.

I recently wanted to read from a XML file which has many optional elements and therefore can occure very differing. I stumbled upon #gotoChild(String xPath) which enables the traversal to a specific child. This is really great but unfortunately it throws an Exception if this child doesn't exists. So I found the #hasTag(String xPath) method which can check this. This works great but bloats up my code extremely.

For example I have the following XML document with the root "user" and the element "name" but it also could optionally contain "login" and "password".

  <user>
    <name>moo</name>
  </foo>

To check this the code would look like

  if(user.hasTag("name")) 
  {
     String name = user.gotoChild("name").getText()
  }
  if(user.hasTag("login")) 
  {
     String login = user.gotoChild("login").getText()
  }

[...]
... and so on!

I would like to have something like Groovys XML slurper. It provides unavaiable nodes/tags if you try to traverse to structured which aren't available.

The code could look like this:

String name = user.gotoChild("name").getText()

Instead of throwing a exception here you could collect the state and return an unavailable tag which returns null on the .getText() call. Also if we try to do something like this:

XMLTag address = user.gotoChild("address")

String street = address.gotoChild("street").getText()

Could also work if you provide a unavailable tag which results null in case of unfound structures.

What do you think?

Hope to hear from you!

Best Regards
Chris
Jun 14, 2011 Delete comment Project Member #1 mathieu.carbou
Yes, I also think that having a null object is a very good idea !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant