解析iphone中的XML

时间:2011-07-11 11:19:52

标签: iphone

我使用GDataXMLNode来解析XML文件。我的问题是当我解析时

<TRUSTEDVINFLAG>&#x0;</TRUSTEDVINFLAG>

结果是

  

实体:第102406行:解析器错误:xmlParseCharRef:无效的xmlChar值0         

这是我的代码:

+ (NSString *)dataFilePath:(BOOL)forSave {

    NSString *documentsDirectory = @"/Users/mediatun1/Desktop/XMLTest";
    NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"IAAT.xml"];
    if (forSave || [[NSFileManager defaultManager] fileExistsAtPath:documentsPath]) {
        return documentsPath;
    } else {
        return [[NSBundle mainBundle] pathForResource:@"IAAT" ofType:@"xml"];
    }

}

+ (ME2Archive *)loadParty {

    NSString *filePath = [self dataFilePath:FALSE];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSError *error;


    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    if (doc == nil) { return nil; }
}

3 个答案:

答案 0 :(得分:0)

如错误消息所示,字符&#x0;在XML文档中无效。因此,创建XML文档时可能存在错误,可能与编码问题有关。尝试使用UTF-8或检查文档源中的虚假信息。

示例,如果文档的标题是

<?xml version="1.0" encoding="utf-8" ?>

这是一个读取文件的示例代码

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"xml"]; 
NSData *myData = [NSData dataWithContentsOfFile:filePath]; 

// this is the important part, I'm reading the nsdata as UTF-8
NSString *xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

答案 1 :(得分:0)

您可以使用NSXMLParser或TBXML。你能预先格式化标签的价值吗?或者你有义务使用你收到的东西?

答案 2 :(得分:0)

尝试将行放在xml中,如:

<TRUSTEDVINFLAG><![CDATA[&#x0;]]></TRUSTEDVINFLAG>