2014-09-29 54 views
0

我正在努力从KJV XML文件中显示随机的圣经经文,但我在XML格式化方面遇到了一些问题,因为它基本上有许多数组。它有这样的格式:iOS从XML文件中抓取随机文本

<?xml version="1.0"?> 
<bible translation="King James Version"> 
    <testament name="Old"> 
     <book name="Genesis"> 
      <chapter number="1"> 
       <verse number="1"> 
       </verse> 
      </chapter> 
     </book> 
    </testament> 
</bible> 

我试图得到这个转换为一个plist文件,以更好地处理它,但我遇到了在这一方面的问题也是如此。我用了一个在线程序将其转换为JSON文件,但使用plutil时从终端转换为plist文件,我得到以下错误:

Unexpected character { at line 1/JSON error: No string key for value in object around character 1. 

JSON格式看起来像这样的前几行:

{ 
    "bible": { 
    "-translation": "King James Version", 
    "testament": [ 
     { 
     "-name": "Old", 
     "book": [ 
      { 
      "-name": "Genesis", 
      "chapter": [ 
       { 
       "-number": "1", 
       "verse": [ 
        { 
        "-number": "1", 
        "#text": "In the beginning God created the heaven and the earth." 
        }, 

我将不胜感激任何建议,无论是在这个转换为plist文件,或使其使用XML。

回答

0

如果使用第三方的lib是没有问题:

使用XML解析器传入的XML解析到一个字典。

https://github.com/nicklockwood/XMLDictionary

加载使用此lib中的XML文件的最简单方法如下:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"xml"]; 
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLFile:filePath]; 

而且转换成字典会给你抓住你想要的任何随机数据的优势。

我希望这会有所帮助。