XML 格式转JSON 格式
#import <Foundation/Foundation.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wobjc-missing-property-synthesis" typedef NS_ENUM(NSInteger, XMLDictionaryAttributesMode)
{
XMLDictionaryAttributesModePrefixed = , //default
XMLDictionaryAttributesModeDictionary,
XMLDictionaryAttributesModeUnprefixed,
XMLDictionaryAttributesModeDiscard
}; typedef NS_ENUM(NSInteger, XMLDictionaryNodeNameMode)
{
XMLDictionaryNodeNameModeRootOnly = , //default
XMLDictionaryNodeNameModeAlways,
XMLDictionaryNodeNameModeNever
}; static NSString *const XMLDictionaryAttributesKey = @"__attributes";
static NSString *const XMLDictionaryCommentsKey = @"__comments";
static NSString *const XMLDictionaryTextKey = @"__text";
static NSString *const XMLDictionaryNodeNameKey = @"__name";
static NSString *const XMLDictionaryAttributePrefix = @"_"; @interface XMLDictionaryParser : NSObject <NSCopying> + (XMLDictionaryParser *)sharedInstance; @property (nonatomic, assign) BOOL collapseTextNodes; // defaults to YES
@property (nonatomic, assign) BOOL stripEmptyNodes; // defaults to YES
@property (nonatomic, assign) BOOL trimWhiteSpace; // defaults to YES
@property (nonatomic, assign) BOOL alwaysUseArrays; // defaults to NO
@property (nonatomic, assign) BOOL preserveComments; // defaults to NO
@property (nonatomic, assign) BOOL wrapRootNode; // defaults to NO @property (nonatomic, assign) XMLDictionaryAttributesMode attributesMode;
@property (nonatomic, assign) XMLDictionaryNodeNameMode nodeNameMode; - (NSDictionary *)dictionaryWithParser:(NSXMLParser *)parser;
- (NSDictionary *)dictionaryWithData:(NSData *)data;
- (NSDictionary *)dictionaryWithString:(NSString *)string;
- (NSDictionary *)dictionaryWithFile:(NSString *)path; @end @interface NSDictionary (XMLDictionary) + (NSDictionary *)dictionaryWithXMLParser:(NSXMLParser *)parser;
+ (NSDictionary *)dictionaryWithXMLData:(NSData *)data;
+ (NSDictionary *)dictionaryWithXMLString:(NSString *)string;
+ (NSDictionary *)dictionaryWithXMLFile:(NSString *)path; - (NSDictionary *)attributes;
- (NSDictionary *)childNodes;
- (NSArray *)comments;
- (NSString *)nodeName;
- (NSString *)innerText;
- (NSString *)innerXML;
- (NSString *)XMLString; - (NSArray *)arrayValueForKeyPath:(NSString *)keyPath;
- (NSString *)stringValueForKeyPath:(NSString *)keyPath;
- (NSDictionary *)dictionaryValueForKeyPath:(NSString *)keyPath; @end @interface NSString (XMLDictionary) - (NSString *)XMLEncodedString; @end #pragma GCC diagnostic pop
//
// XMLDictionary.m
//
// Version 1.4
//
// Created by Nick Lockwood on 15/11/2010.
// Copyright 2010 Charcoal Design. All rights reserved.
//
// Get the latest version of XMLDictionary from here:
//
// https://github.com/nicklockwood/XMLDictionary
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import "XMLDictionary.h"
#pragma GCC diagnostic ignored "-Wobjc-missing-property-synthesis"
#pragma GCC diagnostic ignored "-Wdirect-ivar-access"
#pragma GCC diagnostic ignored "-Wformat-non-iso"
#pragma GCC diagnostic ignored "-Wgnu"
#import <Availability.h>
#if !__has_feature(objc_arc)
#error This class requires automatic reference counting
#endif
@interface XMLDictionaryParser () <NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableDictionary *root;
@property (nonatomic, strong) NSMutableArray *stack;
@property (nonatomic, strong) NSMutableString *text;
@end
@implementation XMLDictionaryParser
+ (XMLDictionaryParser *)sharedInstance
{
static dispatch_once_t once;
static XMLDictionaryParser *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[XMLDictionaryParser alloc] init];
});
return sharedInstance;
}
- (id)init
{
if ((self = [super init]))
{
_collapseTextNodes = YES;
_stripEmptyNodes = YES;
_trimWhiteSpace = YES;
_alwaysUseArrays = NO;
_preserveComments = NO;
_wrapRootNode = NO;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
XMLDictionaryParser *copy = [[[self class] allocWithZone:zone] init];
copy.collapseTextNodes = _collapseTextNodes;
copy.stripEmptyNodes = _stripEmptyNodes;
copy.trimWhiteSpace = _trimWhiteSpace;
copy.alwaysUseArrays = _alwaysUseArrays;
copy.preserveComments = _preserveComments;
copy.attributesMode = _attributesMode;
copy.nodeNameMode = _nodeNameMode;
copy.wrapRootNode = _wrapRootNode;
return copy;
}
- (NSDictionary *)dictionaryWithParser:(NSXMLParser *)parser
{
[parser setDelegate:self];
[parser parse];
id result = _root;
_root = nil;
_stack = nil;
_text = nil;
return result;
}
- (NSDictionary *)dictionaryWithData:(NSData *)data
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
return [self dictionaryWithParser:parser];
}
- (NSDictionary *)dictionaryWithString:(NSString *)string
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [self dictionaryWithData:data];
}
- (NSDictionary *)dictionaryWithFile:(NSString *)path
{
NSData *data = [NSData dataWithContentsOfFile:path];
return [self dictionaryWithData:data];
}
+ (NSString *)XMLStringForNode:(id)node withNodeName:(NSString *)nodeName
{
if ([node isKindOfClass:[NSArray class]])
{
NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:[node count]];
for (id individualNode in node)
{
[nodes addObject:[self XMLStringForNode:individualNode withNodeName:nodeName]];
}
return [nodes componentsJoinedByString:@"\n"];
}
else if ([node isKindOfClass:[NSDictionary class]])
{
NSDictionary *attributes = [(NSDictionary *)node attributes];
NSMutableString *attributeString = [NSMutableString string];
for (NSString *key in [attributes allKeys])
{
[attributeString appendFormat:@" %@=\"%@\"", [[key description] XMLEncodedString], [[attributes[key] description] XMLEncodedString]];
}
NSString *innerXML = [node innerXML];
if ([innerXML length])
{
return [NSString stringWithFormat:@"<%1$@%2$@>%3$@</%1$@>", nodeName, attributeString, innerXML];
}
else
{
return [NSString stringWithFormat:@"<%@%@/>", nodeName, attributeString];
}
}
else
{
return [NSString stringWithFormat:@"<%1$@>%2$@</%1$@>", nodeName, [[node description] XMLEncodedString]];
}
}
- (void)endText
{
if (_trimWhiteSpace)
{
_text = [[_text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
}
if ([_text length])
{
NSMutableDictionary *top = [_stack lastObject];
id existing = top[XMLDictionaryTextKey];
if ([existing isKindOfClass:[NSArray class]])
{
[existing addObject:_text];
}
else if (existing)
{
top[XMLDictionaryTextKey] = [@[existing, _text] mutableCopy];
}
else
{
top[XMLDictionaryTextKey] = _text;
}
}
_text = nil;
}
- (void)addText:(NSString *)text
{
if (!_text)
{
_text = [NSMutableString stringWithString:text];
}
else
{
[_text appendString:text];
}
}
- (void)parser:(__unused NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(__unused NSString *)namespaceURI qualifiedName:(__unused NSString *)qName attributes:(NSDictionary *)attributeDict
{
[self endText];
NSMutableDictionary *node = [NSMutableDictionary dictionary];
switch (_nodeNameMode)
{
case XMLDictionaryNodeNameModeRootOnly:
{
if (!_root)
{
node[XMLDictionaryNodeNameKey] = elementName;
}
break;
}
case XMLDictionaryNodeNameModeAlways:
{
node[XMLDictionaryNodeNameKey] = elementName;
break;
}
case XMLDictionaryNodeNameModeNever:
{
break;
}
}
if ([attributeDict count])
{
switch (_attributesMode)
{
case XMLDictionaryAttributesModePrefixed:
{
for (NSString *key in [attributeDict allKeys])
{
node[[XMLDictionaryAttributePrefix stringByAppendingString:key]] = attributeDict[key];
}
break;
}
case XMLDictionaryAttributesModeDictionary:
{
node[XMLDictionaryAttributesKey] = attributeDict;
break;
}
case XMLDictionaryAttributesModeUnprefixed:
{
[node addEntriesFromDictionary:attributeDict];
break;
}
case XMLDictionaryAttributesModeDiscard:
{
break;
}
}
}
if (!_root)
{
_root = node;
_stack = [NSMutableArray arrayWithObject:node];
if (_wrapRootNode)
{
_root = [NSMutableDictionary dictionaryWithObject:_root forKey:elementName];
[_stack insertObject:_root atIndex:];
}
}
else
{
NSMutableDictionary *top = [_stack lastObject];
id existing = top[elementName];
if ([existing isKindOfClass:[NSArray class]])
{
[existing addObject:node];
}
else if (existing)
{
top[elementName] = [@[existing, node] mutableCopy];
}
else if (_alwaysUseArrays)
{
top[elementName] = [NSMutableArray arrayWithObject:node];
}
else
{
top[elementName] = node;
}
[_stack addObject:node];
}
}
- (NSString *)nameForNode:(NSDictionary *)node inDictionary:(NSDictionary *)dict
{
if (node.nodeName)
{
return node.nodeName;
}
else
{
for (NSString *name in dict)
{
id object = dict[name];
if (object == node)
{
return name;
}
else if ([object isKindOfClass:[NSArray class]] && [object containsObject:node])
{
return name;
}
}
}
return nil;
}
- (void)parser:(__unused NSXMLParser *)parser didEndElement:(__unused NSString *)elementName namespaceURI:(__unused NSString *)namespaceURI qualifiedName:(__unused NSString *)qName
{
[self endText];
NSMutableDictionary *top = [_stack lastObject];
[_stack removeLastObject];
if (!top.attributes && !top.childNodes && !top.comments)
{
NSMutableDictionary *newTop = [_stack lastObject];
NSString *nodeName = [self nameForNode:top inDictionary:newTop];
if (nodeName)
{
id parentNode = newTop[nodeName];
if (top.innerText && _collapseTextNodes)
{
if ([parentNode isKindOfClass:[NSArray class]])
{
parentNode[[parentNode count] - ] = top.innerText;
}
else
{
newTop[nodeName] = top.innerText;
}
}
else if (!top.innerText && _stripEmptyNodes)
{
if ([parentNode isKindOfClass:[NSArray class]])
{
[parentNode removeLastObject];
}
else
{
[newTop removeObjectForKey:nodeName];
}
}
else if (!top.innerText && !_collapseTextNodes && !_stripEmptyNodes)
{
top[XMLDictionaryTextKey] = @"";
}
}
}
}
- (void)parser:(__unused NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self addText:string];
}
- (void)parser:(__unused NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
[self addText:[[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]];
}
- (void)parser:(__unused NSXMLParser *)parser foundComment:(NSString *)comment
{
if (_preserveComments)
{
NSMutableDictionary *top = [_stack lastObject];
NSMutableArray *comments = top[XMLDictionaryCommentsKey];
if (!comments)
{
comments = [@[comment] mutableCopy];
top[XMLDictionaryCommentsKey] = comments;
}
else
{
[comments addObject:comment];
}
}
}
@end
@implementation NSDictionary(XMLDictionary)
+ (NSDictionary *)dictionaryWithXMLParser:(NSXMLParser *)parser
{
return [[[XMLDictionaryParser sharedInstance] copy] dictionaryWithParser:parser];
}
+ (NSDictionary *)dictionaryWithXMLData:(NSData *)data
{
return [[[XMLDictionaryParser sharedInstance] copy] dictionaryWithData:data];
}
+ (NSDictionary *)dictionaryWithXMLString:(NSString *)string
{
return [[[XMLDictionaryParser sharedInstance] copy] dictionaryWithString:string];
}
+ (NSDictionary *)dictionaryWithXMLFile:(NSString *)path
{
return [[[XMLDictionaryParser sharedInstance] copy] dictionaryWithFile:path];
}
- (NSDictionary *)attributes
{
NSDictionary *attributes = self[XMLDictionaryAttributesKey];
if (attributes)
{
return [attributes count]? attributes: nil;
}
else
{
NSMutableDictionary *filteredDict = [NSMutableDictionary dictionaryWithDictionary:self];
[filteredDict removeObjectsForKeys:@[XMLDictionaryCommentsKey, XMLDictionaryTextKey, XMLDictionaryNodeNameKey]];
for (NSString *key in [filteredDict allKeys])
{
[filteredDict removeObjectForKey:key];
if ([key hasPrefix:XMLDictionaryAttributePrefix])
{
filteredDict[[key substringFromIndex:[XMLDictionaryAttributePrefix length]]] = self[key];
}
}
return [filteredDict count]? filteredDict: nil;
}
return nil;
}
- (NSDictionary *)childNodes
{
NSMutableDictionary *filteredDict = [self mutableCopy];
[filteredDict removeObjectsForKeys:@[XMLDictionaryAttributesKey, XMLDictionaryCommentsKey, XMLDictionaryTextKey, XMLDictionaryNodeNameKey]];
for (NSString *key in [filteredDict allKeys])
{
if ([key hasPrefix:XMLDictionaryAttributePrefix])
{
[filteredDict removeObjectForKey:key];
}
}
return [filteredDict count]? filteredDict: nil;
}
- (NSArray *)comments
{
return self[XMLDictionaryCommentsKey];
}
- (NSString *)nodeName
{
return self[XMLDictionaryNodeNameKey];
}
- (id)innerText
{
id text = self[XMLDictionaryTextKey];
if ([text isKindOfClass:[NSArray class]])
{
return [text componentsJoinedByString:@"\n"];
}
else
{
return text;
}
}
- (NSString *)innerXML
{
NSMutableArray *nodes = [NSMutableArray array];
for (NSString *comment in [self comments])
{
[nodes addObject:[NSString stringWithFormat:@"<!--%@-->", [comment XMLEncodedString]]];
}
NSDictionary *childNodes = [self childNodes];
for (NSString *key in childNodes)
{
[nodes addObject:[XMLDictionaryParser XMLStringForNode:childNodes[key] withNodeName:key]];
}
NSString *text = [self innerText];
if (text)
{
[nodes addObject:[text XMLEncodedString]];
}
return [nodes componentsJoinedByString:@"\n"];
}
- (NSString *)XMLString
{
if ([self count] == && ![self nodeName])
{
//ignore outermost dictionary
return [self innerXML];
}
else
{
return [XMLDictionaryParser XMLStringForNode:self withNodeName:[self nodeName] ?: @"root"];
}
}
- (NSArray *)arrayValueForKeyPath:(NSString *)keyPath
{
id value = [self valueForKeyPath:keyPath];
if (value && ![value isKindOfClass:[NSArray class]])
{
return @[value];
}
return value;
}
- (NSString *)stringValueForKeyPath:(NSString *)keyPath
{
id value = [self valueForKeyPath:keyPath];
if ([value isKindOfClass:[NSArray class]])
{
value = [value count]? value[]: nil;
}
if ([value isKindOfClass:[NSDictionary class]])
{
return [(NSDictionary *)value innerText];
}
return value;
}
- (NSDictionary *)dictionaryValueForKeyPath:(NSString *)keyPath
{
id value = [self valueForKeyPath:keyPath];
if ([value isKindOfClass:[NSArray class]])
{
value = [value count]? value[]: nil;
}
if ([value isKindOfClass:[NSString class]])
{
return @{XMLDictionaryTextKey: value};
}
return value;
}
@end
@implementation NSString (XMLDictionary)
- (NSString *)XMLEncodedString
{
return [[[[[self stringByReplacingOccurrencesOfString:@"&" withString:@"&"]
stringByReplacingOccurrencesOfString:@"<" withString:@"<"]
stringByReplacingOccurrencesOfString:@">" withString:@">"]
stringByReplacingOccurrencesOfString:@"\"" withString:@"""]
stringByReplacingOccurrencesOfString:@"\'" withString:@"'"];
}
@end
XML 格式转JSON 格式的更多相关文章
- ajax处理返回的三种格式(json格式 , xml通用格式 , html文本格式)(数据类型:整数、字符串、数组、对象)(基础最重要!)
ajax方法的参数 常用的ajax参数比如url,data,type,包括预期返回类型dataType,发送到服务器的数据的编码类型contentType,成功方法,失败方法,完成方法.除了这些以外还 ...
- java中集合格式及json格式的特点和转换
作者原创:转载请注明出处 今天在写代码,遇到一个难点,由于要调用webservice接口,返回的为一个list集合内容,从webservice调用接口返回的为一个string的io流, 在调用接口的地 ...
- 将object格式转为json格式
在页面内容显示时,有时需要用到json格式.但数据库内容的显示,需要将数据库中获取的格式转为json: using Newtonsoft.Json;public static string ToJso ...
- 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。
http://zhengxinlong.iteye.com/blog/848712 将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name& ...
- python中将xml格式转json格式
一.简介 在用python写脚本时,通常需要处理xml格式的文件或字符串.由于json格式处理的方便性, 我们可将其转为json格式进行处理. 二.步骤 1.安装工具包xmltodict 在命令行输入 ...
- Java对象的序列化与反序列化:默认格式及JSON格式(使用jackson)
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3558663.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- ajax 提交form格式 和 json格式
json 格式 内容在body中 ajax设置 Content-Type: application/json 浏览器查看为 Request Payload The Request Payload ...
- 数据可视化:CSV格式,JSON格式
下载CSV格式数据,进行可视化 csv.reader()创建一个与文件有关联的阅读器(reader)对象,reader处理文件中的第一行数据,并将每一项数据都存储在列表中 head_row = nex ...
- Java 字符串(String)格式转json格式
json是前后端传输数据的一种文本格式,json其实就是字符串,因为前后端传输数据时,只能传输字符串,我们又想传一些对象或者列表信息,这都是很常见的应用场景. 所以,我们需要在java代码中,把jav ...
- postman做接口测试 application/x-www-form-urlencoded 格式与json格式互转
背景:用postman做接口测试可以使用application/x-www-form-urlencoded请求,也可以使用json请求,接口文档如下: 请求参数 字段 类型 是否必填 注释 websi ...
随机推荐
- Things App Engine Doesn't Do...Yet
当人们第一次使用App Engine的时候,他们会问一些App Engine不会做的事情.其中的一些事情Google在不久的将来会实现的,还有一些违背了App Engine设计的本质,将不可能增加(到 ...
- python setup.py install 失败
由于curl证书太老,所以无法找到一些对应的版本. 如下更新证书即可: curl http://curl.haxx.se/ca/cacert.pem > /etc/pki/tls/certs/c ...
- Fast Intro To Java Programming (2)
Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...
- Todolist
UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)
- Struts2的输入验证
一.概述: ① Struts2的输入验证 –基于 XWorkValidation Framework的声明式验证:Struts2提供了一些基于 XWork Validation Framework的内 ...
- 排序算法之快速排序(java实现)
package com.javaTest300; public class Test039 { public static void main(String[] args) {// 快速排序 int ...
- OAuth2-Server-php
Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...
- Android流量监控 思路,想法
1,开启一个服务,每5分钟跑动一次更新流量,用于能够准确记录流量 每一个小时,更新一次流量,用于清除非本月的流量 2,保存流量的时候,进行判断 a,若是数据库中保存的 ...
- hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...
- Squid代理服务器&&搭建透明代理网关服务器
案例需求 ——公司选用RHEL5服务器作为网关,为了有效节省网络带宽.提高局域网访问Internet的速度,需要在网关服务器上搭建代理服务,并结合防火墙策略实现透明代理,以减少客户端的重复设置工作 需 ...