简体   繁体   English

在iOS中解析多属性值

[英]Parse multi attribute values in iOS

I am developing an application and i want to parse the xml but when it comes in the way of multiple attribute then how will u do it using the nsxmlparser 我正在开发一个应用程序,我想解析xml,但是当它以多重属性的方式出现时,您将如何使用nsxmlparser来做到这一点?

 <?xml version="1.0" encoding='utf-8' ?>
<Responses Id='7465'>
<Response RequestID="101" FunctionStatus="0" Message="OK"/>

<Result>

<PrimaryID>
<Item ID="1" Text="Aadhar Card"/>
<Item ID="2" Text="Voters ID"/>
<Item ID="3" Text="Driving Licence"/>
<Item ID="4" Text="PAN Card"/>
<Item ID="5" Text="Passport"/>
</PrimaryID>

<Sex>
<Item ID="1" Text="Male"/>
<Item ID="2" Text="Female"/>
</Sex>

<Title>
<Item ID="1" Text="Mr"/>
<Item ID="2" Text="Ms"/>
<Item ID="3" Text="Mrs"/>
</Title>
</Result>

</Responses>

With XMLReader you can get it in a dictionary like this : 使用XMLReader,您可以像这样在字典中获取它:

file.xml in your resource bundle : 资源包中的file.xml:

- (void)dataFromXML
{
    // Error
    NSError *error = nil;

    // Request
    NSMutableDictionary *xmlDic = nil;
    NSData *xmlData = nil;

    // Get XML
    xmlData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"file" ofType:@"xml"]];

    // Parse the XML Data into a NSDictionary
    xmlDic = [NSMutableDictionary dictionaryWithDictionary:[XMLReader dictionaryForXMLData:xmlData error:&error]];

    // Configure Dictionary
    xmlDic = [[[xmlDic objectForKey:@"Responses"] objectForKey:@"Response"] objectForKey:@"Result"];
    NSLog(@"%@", xmlDic);
}

file.xml on the web : 网络上的file.xml:

- (void)dataWSFromXML
{
    // Error
    NSError *error = nil;

    // Request
    NSURLRequest *request = nil;
    NSURLResponse *response = nil;
    NSMutableDictionary *xmlDic = nil;
    NSData *xmlData = nil;

    // Get XML
    request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourXMLfile.xml"]];
    xmlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // You can improve this request

    // Parse the XML Data into a NSDictionary
    xmlDic = [NSMutableDictionary dictionaryWithDictionary:[XMLReader dictionaryForXMLData:xmlData error:&error]];

    // Configure Dictionary
    xmlDic = [[[xmlDic objectForKey:@"Responses"] objectForKey:@"Response"] objectForKey:@"Result"];
    NSLog(@"%@", xmlDic);
}

But I change your XML like this : 但是我这样更改您的XML:

<?xml version="1.0" encoding="UTF-8"?>
<Responses Id='7465'>
    <Response RequestID="101" FunctionStatus="0" Message="OK">
        <Result>

            <PrimaryID>
                <Item ID="1" Text="Aadhar Card"/>
                <Item ID="2" Text="Voters ID"/>
                <Item ID="3" Text="Driving Licence"/>
                <Item ID="4" Text="PAN Card"/>
                <Item ID="5" Text="Passport"/>
            </PrimaryID>

            <Sex>
                <Item ID="1" Text="Male"/>
                <Item ID="2" Text="Female"/>
            </Sex>

            <Title>
                <Item ID="1" Text="Mr"/>
                <Item ID="2" Text="Ms"/>
                <Item ID="3" Text="Mrs"/>
            </Title>
        </Result>
    </Response>
</Responses>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM