Вопрос

Я пытаюсь проанализировать приведенный ниже XML с помощью TBXML. Я хочу создать массив, который содержит все значения тега элемента. Как я могу пройти через теги «элемент»?

<root>
<item>
<northeast_area>
<item>
<new_england_north xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[8]">
    <item xsi:type="xsd:string">boston s, ma</item>
    <item xsi:type="xsd:string">providence, ri</item>
    <item xsi:type="xsd:string">boston, ma </item>
    <item xsi:type="xsd:string">portland, me</item>
    <item xsi:type="xsd:string">boston, ma </item>
    <item xsi:type="xsd:string">boston central</item>
    <item xsi:type="xsd:string">boston north, ma</item>
    <item xsi:type="xsd:string">boston south, ma</item>
</new_england_north>
</item>

<item>
<upstate_new_york xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[6]">
    <item xsi:type="xsd:string">binghampton, ny</item>
    <item xsi:type="xsd:string">rochester, ny</item>
    <item xsi:type="xsd:string">albany s, ny</item>
    <item xsi:type="xsd:string">syracuse, ny</item>
    <item xsi:type="xsd:string">albany, ny</item>
    <item xsi:type="xsd:string">buffalo, ny</item>
</upstate_new_york>
</item>
</northeast_area>
</item>

Это было полезно?

Решение

Вот кусок образца кода, который считывает XML из файла. Вы можете настроить его, чтобы использовать доступный объект NSString/NSDATA. Примечание что, поскольку мы не знаем точных потенциальных форматов, которые могут вернуть ваши мыльные звонки, это не очень надежно, Это просто решение, которое работает для предоставленных вами данных.

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        NSString* path = @"/Path/To/Your/File";
        NSData *contents = [[NSFileManager defaultManager] contentsAtPath:path];
        TBXML *xml = [TBXML tbxmlWithXMLData:contents];

        TBXMLElement *rootElement = [xml rootXMLElement];

        TBXMLElement *rootItemElem = [TBXML childElementNamed:@"item" parentElement:rootElement];

        while (rootItemElem != nil) {
            TBXMLElement *areaElement = rootItemElem->firstChild;

            while (areaElement != nil) {
                TBXMLElement *areaItemElement = [TBXML childElementNamed:@"item" parentElement:areaElement];
                while (areaItemElement != nil) {
                    TBXMLElement *localeItem = areaItemElement->firstChild;
                    NSLog(@"Checking locale %s", localeItem->name);
                    TBXMLElement *specificItem = [TBXML childElementNamed:@"item" parentElement:localeItem];
                    while (specificItem != nil) {
                        NSLog(@"Item with value: %@", [TBXML textForElement:specificItem]);
                        specificItem = [TBXML nextSiblingNamed:@"item" searchFromElement:specificItem];
                    }

                    areaItemElement = [TBXML nextSiblingNamed:@"item" searchFromElement:areaItemElement];
                }

                areaElement = areaElement->nextSibling;
            }

            rootItemElem = [TBXML nextSiblingNamed:@"item" searchFromElement:rootItemElem];
        }

    }
    return 0;
}

И вывод:

2011-10-19 08:20:40.321 Testing[14768:707] Checking locale new_england_north
2011-10-19 08:20:40.323 Testing[14768:707] Item with value: boston s, ma
2011-10-19 08:20:40.323 Testing[14768:707] Item with value: providence, ri
2011-10-19 08:20:40.324 Testing[14768:707] Item with value: boston, ma
2011-10-19 08:20:40.324 Testing[14768:707] Item with value: portland, me
2011-10-19 08:20:40.325 Testing[14768:707] Item with value: boston, ma
2011-10-19 08:20:40.326 Testing[14768:707] Item with value: boston central
2011-10-19 08:20:40.326 Testing[14768:707] Item with value: boston north, ma
2011-10-19 08:20:40.327 Testing[14768:707] Item with value: boston south, ma
2011-10-19 08:20:40.327 Testing[14768:707] Checking locale upstate_new_york
2011-10-19 08:20:40.328 Testing[14768:707] Item with value: binghampton, ny
2011-10-19 08:20:40.328 Testing[14768:707] Item with value: rochester, ny
2011-10-19 08:20:40.329 Testing[14768:707] Item with value: albany s, ny
2011-10-19 08:20:40.329 Testing[14768:707] Item with value: syracuse, ny
2011-10-19 08:20:40.330 Testing[14768:707] Item with value: albany, ny
2011-10-19 08:20:40.330 Testing[14768:707] Item with value: buffalo, ny

Используя это, должно быть довольно просто создать NSArray и хранить предметы или создать NSArray Для каждого локали и отделяйте элементы таким образом. (Храня их в NSDictionary Ключ по месту.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top