Issue 65: How to get the namespace declaration string value from an xml file ?
Status:  WontFix
Owner: ----
Closed:  Aug 2010
Reported by zhus...@gmail.com, Oct 19, 2009
xml string:
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

How to get the string value: "http://search.yahoo.com/mrss/" ?

Nov 4, 2009
#1 josephhu...@gmail.com
Hello here is an example of what I did until Namespaces are supported

//CXMLElement+Namespaces.h
#import <Foundation/Foundation.h>
#import "CXMLElement.h"

@interface CXMLElement (Namespaces)

-(NSString*)ns;
-(NSDictionary*)namespaces;

@end


//CXMLElement+Namespaces.m
#import "CXMLElement+Namespaces.h"
#import "CXMLNode_PrivateExtensions.h"


@implementation CXMLElement (Namespaces)

-(NSString*)ns
{
	if(_node->ns == NULL)
		return nil;
	return [NSString stringWithUTF8String:(const char*)((xmlNs*)_node->ns)->href];
}

-(NSDictionary*)namespaces
{
	NSMutableDictionary *theNamespaces = [NSMutableDictionary dictionary];
	xmlNsPtr theCurrentNode = _node->nsDef;
	while (theCurrentNode != NULL)
	{
		xmlNsPtr ns = (xmlNsPtr)theCurrentNode;
		[theNamespaces setValue:[NSString stringWithUTF8String:(const char*)ns->href] 
						 forKey:[NSString stringWithUTF8String:(const char*)ns->prefix]];

		theCurrentNode = theCurrentNode->next;
	}
	return(theNamespaces);
}

@end

Nov 4, 2009
#2 josephhu...@gmail.com
To use those class just create a CXMLElement+Namespaces.h and a CXMLElement+Namespaces.m file with the respective content then import CXMLElement+Namespaces.h in 
the code you need it. Once you have the CXMLNode for <rss ....> cast it as CXMLElement and then you can access the namespaces.

Ex.

if([[node name] isEqualToString:@"rss"])
{
	CXMLElement *ele = (CXMLElement*)node;
	NSString* ns = [ele ns];//this is the current namespace of the element
	NSDictionary *namespaces = [ele namespaces];
	for(NSString *key in namespaces)
	{
		//key = namespace prefix.  atom, wfw, content, etc.
		//href = uri. "http://search.yahoo.com/mrss/"
		NSString* href = [namespaces valueForKey:key];
	}
}

Aug 11, 2010
Project Member #3 jwight
(No comment was entered for this change.)
Status: WontFix
Labels: TouchXML