iphone - 使用XMLReader从XML到JSON删除文本

标签 iphone ios xml json

我正在使用下面的代码将XMLString转换为JSONstring。我还尝试通过替换换行符和空格将该XML转换为单行,但是输出没有任何变化。

NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&parseError];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&error];
if (! jsonData)
{
    NSLog(@"Got an error: %@", error);
}
else
{
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

这是我的示例XMLString
<component>
        <section ID="id_0b9b2dbb-ac86-4aad-b567-00a78d9e18a7">
           <id root="7d973a3a-d4fa-440c-b9fd-7bf0f9bfc1c4"/>
           <code code="34066-1" codeSystem="2.16.840.1.113883.6.1" displayName="Boxed Warning section"/>
           <title>WARNING</title>
           <text>
              <paragraph>Suicidality and Antidepressant Drugs</paragraph>
              <paragraph>
                 <content styleCode="bold">
                    <content styleCode="italics">Use in Treating Psychiatric Disorders:        </content>
                 </content> Antidepressants increased the risk compared to placebo of suicidal thinking and behavior (suicidality) in children, adolescents, and young adults in short-term studies of major depressive disorder (MDD) and other psychiatric disorders. Anyone considering the use of WELLBUTRIN XL<sup>®</sup>* or any other antidepressant in a child, adolescent, or young adult must balance this risk with the clinical need. Short-term studies did not show an increase in the risk of suicidality with antidepressants compared to placebo in adults beyond age 24; there was a reduction in risk with antidepressants compared to placebo in adults aged 65 and older. Depression and certain other psychiatric disorders are themselves associated with increases in the risk of suicide. Patients of all ages who are started on antidepressant therapy should be monitored appropriately and observed closely for clinical worsening, suicidality, or unusual changes in behavior. Families and caregivers should be advised of the need for close observation and communication with the prescriber. WELLBUTRIN XL is not approved for use in pediatric patients. (See <content styleCode="bold"<linkHtml href="#i4i_warnings_id_2dfa5d10-0696-41d8-bc4b-d82cd933a761">WARNINGS: Clinical Worsening and Suicide Risk in Treating Psychiatric Disorders</linkHtml>, <linkHtml href="#i4i_precautions_id_8611ce24-cbea-4cf8-81fe-3efe8c368279">PRECAUTIONS: Information for Patients</linkHtml>, and <linkHtml href="#i4i_precautions_id_8611ce24-cbea-4cf8-81fe-3efe8c368279">PRECAUTIONS: Pediatric Use</linkHtml>.)</content>
</paragraph>
</text>
<effectiveTime value="20100602"/>
</section>
</component>

这是我的JSON输出:
"component" : [
            {
              "section" : {
                "ID" : "Treating",
                "id" : {
                  "text" : "\n               \n                  \n                   ",
                  "root" : "2724b846-267b-4dbb-bd9b-ec4db83617ff"
                },
                "code" : {
                  "code" : "42229-5",
                  "displayName" : "SPL UNCLASSIFIED SECTION",
                  "text" : "\n                     ",
                  "codeSystem" : "2.16.840.1.113883.6.1"
                },
"title" : {
                  "text" : "\n                     Suicidality and Antidepressant Drugs"
                },
                "text" : "\n                  ",
                "effectiveTime" : {
                  "value" : "20120823",
                  "text" : "\n                     "
                }
              },
              "text" : "\n               "
            },

我得到了这个JSON;请帮助获取具有正确数据的正确JSON。我没有收到文本,您可以看到:
"text" : "\n               \n                  \n                   ",

最佳答案

或者,可以使用XMLConverter代替XMLReader。

关于iphone - 使用XMLReader从XML到JSON删除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629638/

相关文章:

ios - UIButton 在 TableView 单元格中不可见

c++ - "save"的设计模式

php - 使用 xpath 从 xml 中获取值

ios - 如何在iPhone上测试和调试Xamarin跨平台应用程序?

ios - 作为调试运行与作为发布运行(仪器作为发布运行)时的 CPU 使用率较高。如何调试?

ios - UITableViewCell 度假村无需重新加载整个表格

ios - Stockfish Chess Engine 与 Swift 中的 iOS 项目集成

c# - xml 对象中缺少信封标记

iphone - 使用NSUserDefaults存储很多值会出问题?

ios - "Not running"状态下的远程通知