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

Parsing XML with namespaces #30

Closed
ajsait opened this issue May 27, 2015 · 6 comments
Closed

Parsing XML with namespaces #30

ajsait opened this issue May 27, 2015 · 6 comments
Assignees

Comments

@ajsait
Copy link

ajsait commented May 27, 2015

Currently, there is no way to set the XMLParser shouldProcessNamespaces to true. So when accessing an element, the complete tagname with namespace needs to be passed. e.g xml["f:animal"] instead of just xml["animal"]

In the example in the code, it would be nice if instead of this

xml["root"]["h:table"]["h:tr"]["h:td"][0].element!.text!

we will have this

xml["root"]["table"]["tr"]["td"][0].element!.text!

@drmohundro
Copy link
Owner

Thanks for the suggestion - I like the idea and it should be relatively easy to support.

Just thinking out loud, but my initial thought is to add an optional options parameter to parse to allow for setting things like this. Any thoughts on that?

@ajsait
Copy link
Author

ajsait commented May 27, 2015

I have no problem with that. I think that would also be my approach.

you reckon how long will it take to add this functionality?

@drmohundro
Copy link
Owner

Sorry for the delayed response - I didn't even see your question regarding how long it would take. It honestly should be a pretty quick change.

I actually had another thought regarding a variation in the API - I thought a fluent API where a config parameter was passed to an action might make a little more sense, like so:

SWXMLHash.configure {
  config in
  config.shouldProcessNamespaces = true
}.parse(xml)

That way, it would be easy to add additional configuration options to set ahead of time as opposed to adding multiple parameters to the parse method. As an aside, it might be a better way to handle the lazy parsing, too.

Quick question for you - are you currently targeting the new Swift 2.0 in beta or are you still on Swift 1.2 (currently released)? I've set up a branch for 2.0 and can make the changes there, but if this would help you on 1.2, I can make the change there and then merge it over.

@ajsait
Copy link
Author

ajsait commented Jun 11, 2015

We are still using swift 1.2

@drmohundro drmohundro self-assigned this Jun 12, 2015
drmohundro added a commit that referenced this issue Jun 12, 2015
This change introduces a new fluent configuration method as a way of
hopefully making it easy and straightforward to introduce new
configuration options in the future.

The first config option introduced in this commit is
"shouldProcessNamespaces" which is forwarded on to the NSXMLParser
instance (related to issue #30).

There is also a config option for lazy XML parsing which can be used
instead of calling "lazy" versus "parse".
@drmohundro
Copy link
Owner

I've created #34 to track this work - there are tests that pass. The general usage is:

parser = SWXMLHash.config { conf in
    conf.shouldProcessNamespaces = true
}.parse(xmlWithNamespace)

I'd like to keep the pull request open for a little while to see if anyone has any additional input on using the config method like that. I've used that approach in the past for my own projects and a few other libraries, but I haven't seen it used much in any Swift projects yet. If I don't hear any feedback, though, I'll go ahead and merge it into master.

@drmohundro
Copy link
Owner

I just merged my #34 to master - the above code snippet should provide what you're looking for. Thanks for the report!

klaaspieter pushed a commit to klaaspieter/SWXMLHash that referenced this issue Nov 18, 2016
Swift 3 & Linux support (not for merging)
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

2 participants