如何在ios中使用Structure解析Xml

时间:2013-06-13 05:35:53

标签: ios

我知道如何解析一些XML结构,但我目前正在尝试解析这个与我习惯的有点不同的特定xml结构。

但是现在我正在使用一些像这样的xml .. 你能帮帮我吗?

<?xml version="1.0" encoding="UTF-8"?>
<EssentialPhoneNumbers>
    <Department>
        <Name>Direct sales department</Name>
        <Phone>03-6380000</Phone>
        <Hours>Sunday - Thursday 20:00 to 8:30. Fridays and holiday eves: 8:00 to 12:00</Hours>
        <Contacts>
            <Contact>
                <Name>Inquiries about purchasing life and health insurance</Name>
                <Phone>03-6380390</Phone>
                <Fax>NA</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Inquiries about purchasing car insurance, apartment and office</Name>
                <Phone>03-6380350</Phone>
                <Fax>NA</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Doctors - medical bug reports or claims (MCI Inc.)</Name>
                <Phone>03-6380406</Phone>
                <Fax>03-6380345</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Focus option - all health insurance</Name>
                <Phone>03-6380204</Phone>
                <Fax>03-5244401</Fax>
                <Hours>Sunday - Thursday 20:00 to 8:30</Hours>
            </Contact>
            <Contact>
                <Name>General insurance claims department Avinoam Ike</Name>
                <Phone>03-6380429</Phone>
                <Fax>NA</Fax>
                <Hours>NA</Hours>
            </Contact>
        </Contacts>
    </Department>
    <Department>
        <Name>Service Department</Name>
        <Phone>03-6380350</Phone>
        <Hours>Sunday - Thursday 6:00 p.m. to 8:00 a.m. Fridays and holiday eves: 8:00 to 12:00</Hours>
        <Contacts>
            <Contact>
                <Name>Center and emergency assistance services 24 hours a day</Name>
                <Phone>1-800-280-555</Phone>
                <Fax>NA</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Advice on medico - wavy insured professional liability insurance - medical(MCI Inc. )</Name>
                <Phone>03-6380346</Phone>
                <Fax>03-6380345</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Urgent questions on Medico - Records (other than the hours at the office )</Name>
                <Phone>052-227633</Phone>
                <Fax>NA</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Inquiries about professional liability insurance - medical</Name>
                <Phone>03-6380480</Phone>
                <Fax>03-6380461</Fax>
                <Hours>NA</Hours>
            </Contact>
            <Contact>
                <Name>Team members focus</Name>
                <Phone>1-700-700-251</Phone>
                <Fax>NA</Fax>
                <Hours>Sunday - Thursday 20:00 to 8:00</Hours>
            </Contact>
            <Contact>
                <Name>Inquiries about Medical Risk Management(MRM)</Name>
                <Phone>03-6380458</Phone>
                <Fax>03-6380345</Fax>
                <Hours>NA</Hours>
            </Contact>
        </Contacts>
    </Department>
</EssentialPhoneNumbers>

2 个答案:

答案 0 :(得分:1)

这是使用GDataXML解析此xml的方法

#define kDepartmentName  @"Name"
#define kDepartmentPhone @"Phone"
#define kDepartmentHours @"Hours"

#define kContactName    @"Name"
#define kContactPhone   @"Phone"
#define kContactFax     @"Fax"
#define kContactHours   @"Hours"

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

GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:fileData
                                                          encoding:NSUTF8StringEncoding
                                                             error:nil];
NSArray *departmentNodes = [document nodesForXPath:@"//EssentialPhoneNumbers/Department" error:nil];

NSMutableArray *departments = [NSMutableArray array];
for (GDataXMLElement *departmentElement in departmentNodes) {
    NSMutableDictionary *department = [NSMutableDictionary dictionary];
    NSString *deptName = [[departmentElement elementsForName:kDepartmentName][0] stringValue];
    if (deptName) {
        department[kDepartmentName] = deptName;
    }

    NSString *deptPhone = [[departmentElement elementsForName:kDepartmentPhone][0] stringValue];
    if (deptPhone) {
        department[kDepartmentPhone] = deptPhone;
    }

    NSString *deptHours = [[departmentElement elementsForName:kDepartmentHours][0] stringValue];
    if (deptHours) {
        department[kDepartmentHours] = deptHours;
    }

    NSArray *contactsNodes = [departmentElement nodesForXPath:@"Contacts/Contact" error:nil];
    NSMutableArray *contacts = [NSMutableArray array];
    for (GDataXMLElement *contactElement in contactsNodes) {

        NSMutableDictionary *contact = [NSMutableDictionary dictionary];
        NSString *contactName = [[contactElement elementsForName:kContactName][0] stringValue];
        if (contactName) {
            contact[kContactName] = contactName;
        }

        NSString *contactPhone = [[contactElement elementsForName:kContactPhone][0] stringValue];
        if (contactPhone) {
            contact[kContactPhone] = contactPhone;
        }

        NSString *contactFax = [[contactElement elementsForName:kContactFax][0] stringValue];
        if (contactFax) {
            contact[kContactFax] = contactFax;
        }

        NSString *contactHours = [[contactElement elementsForName:kContactHours][0] stringValue];
        if (contactHours) {
            contact[kContactHours] = contactHours;
        }

        if ([[contact allKeys]count]) {
            [contacts addObject:contact];
        }
    }

    if ([contacts count]) {
        department[@"Contacts"] = contacts;
    }

    if ([[department allKeys]count]) {
        [departments addObject:department];
    }

}

NSLog(@"%@",departments);

Contacts.xml档案

答案 1 :(得分:0)

我正在使用TBXMLParser。

这是我的代码

 dataForParser = [queryResult dataUsingEncoding:NSUTF8StringEncoding];  
    TBXML *tbxml = [TBXML tbxmlWithXMLData: dataForParser];
    TBXMLElement *root = tbxml.rootXMLElement;
    if (root) {
        TBXMLElement *item = [TBXML childElementNamed:@"item" parentElement:root];
            while (item) {
                NSString * type = [NSString stringWithFormat:[TBXML valueOfAttributeNamed:@"type" forElement:item]];
                if ([type isEqualToString:@"0"]) { 

                    Chair *ch = [[Chair alloc] init];

                    NSMutableArray * chairsAr = [[NSMutableArray alloc] init];
                    [chairsAr addObject: type];

                    ch.chairPic = [NSString stringWithFormat:[TBXML valueOfAttributeNamed:@"picture" forElement:item]];
                    ch.xChObject = [NSString stringWithFormat:[TBXML valueOfAttributeNamed:@"x" forElement:item]];
                    ch.yChObject = [NSString stringWithFormat:[TBXML valueOfAttributeNamed:@"y" forElement:item]];
                    ch.chairAngle = [NSString stringWithFormat:[TBXML valueOfAttributeNamed:@"angle" forElement:item]];

                    [allChairs addObject:ch];