Frage

Ich habe ein XML mit der folgenden Struktur und versuche, mein Modellobjekt daraus zu erstellen. Kann mir bitte jemand helfen, einen Weg zu finden, diese Objekte mit TouchXML, NSmutablearray und NSmutableDictionay aus dem XML zu erhalten?

<?xml version="1.0" encoding="utf-8"?>
<response>
  <level1_items>
    <level1_item>
      <item_1>text</item_1>
      <item_2>text</item_2>
    </level1_item>
    <level1_item>
      <item_1>some text</item_1>
      <item_2>some more text</item_2>
  </level1_items>
  <items>
    <item>
      <child_items>
        <child_item>
          <leaf1>node text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>some text</leaf3>
        </child_item>
        <child_item>
          <leaf1>text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>more text</leaf3>
        </child_item>
      </child_items>
    </item>
    <item>
      <child_items>
        <child_item>
          <leaf1>node text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>some text</leaf3>
        </child_item>
        <child_item>
          <leaf1>text</leaf1>
          <leaf2>leaf text</leaf2>
          <leaf3>more text</leaf3>
        </child_item>
      </child_items>
    </item>
  </items>
</response>

Ich muss das analysieren <response> und seine Kinder.

War es hilfreich?

Lösung

Zunächst muss ich sagen, dass Ihr XML schwer zu arbeiten ist, da Sie versuchen, ein bestimmtes Tag für jedes Element in einer Liste zu verwenden. Zum Beispiel:

  <level1_items>
    <level1_item>
      <item_1>text</item_1>
      <item_2>text</item_2>
    </level1_item>
    <level1_item>
      <item_1>some text</item_1>
      <item_2>some more text</item_2>
  </level1_items>

Die Tags genanntu003Citem_ 1> Anwesendu003Citem_ 2> Machen Sie keinen Sinn, da XML Ihre Daten beschreiben soll und nicht definieren soll. Wenn Sie eine Entität namens Artikel haben, sollten Sie es wahrscheinlich einfach anrufen Artikel Und lassen Sie den XML so aussehen:

  <level1_items>
    <level1_item>
      <item index="1">text</item>
      <item index="2">text</item>
    </level1_item>
    <level1_item>
      <item index="1">some text</item>
      <item index="2">some more text</item>
  </level1_items>

Wo jeweils Artikel hat einen Index. In diesem Fall können Sie das Dokument einfach in Ihr TouchXML CXMLDOCument laden und die Knoten mit XPath greifen und davon ausgehen, dass sie in der richtigen Reihenfolge den von mir angegebenen Index = Parameter ignorieren.

Das nächste Problem ist, dass die Konvertierung von XML in eine NSDICTIOANRY oder NSArray von Wörterbüchern eine ziemlich engagierte Aufgabe ist und was Sie gewinnen, ist die Mühe nicht wert. Laden Sie Ihr XML einfach in ein CXMLDOCUMEN und beginnen Sie mit XPath Knoten. Etwas wie das:

CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];

// Returns all 'level1_item' nodes in an array    
NSArray *nodes = [[doc rootElement] nodesForXPath:@"//response/level1_items/level1_item" error:nil];

for (CXMLNode *itemNode in nodes)
{
    for (CXMLNode *childNode in [itemNode children])
    {
        NSString *nodeName = [childNode name]; // should contain item_1 in first iteration
        NSString *nodeValue = [childNode stringValue]; // should contain 'text' in first iteration
        // Do something with the node data.

    }
}

Ich schlage vor, den XML auf diese Weise zu verwenden und dann hierher zurückzukehren und spezifische Fragen zu stellen, wenn Sie Probleme haben.

Andere Tipps

    -(NSMutableArray *)parseNodeFromXML:(NSString *)strXML readForNode:(NSString *)strReadValue
    {
        NSError *theError = NULL;
        CXMLDocument *theXMLDocument = [[CXMLDocument alloc] initWithXMLString:strXML options:0 error:&theError] ;

        NSMutableArray *arrReturn = [[NSMutableArray alloc] init];
        NSArray *nodes = [[theXMLDocument rootElement] nodesForXPath:strReadValue error:nil];

        for (CXMLNode *itemNode in nodes)
        {

            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

            // PROCESS FOR READ ELEMENT ATTRIBUTES IN CURRENT NODE -------------------------

            if([itemNode isMemberOfClass:[CXMLElement class]]){
                CXMLElement *elements=(CXMLElement*)itemNode;
                NSArray *arAttr=[elements attributes];

                for (int i=0; i<[arAttr count]; i++) {
                    if([[arAttr objectAtIndex:i] name] && [[arAttr objectAtIndex:i] stringValue]){
                        [dic setValue:[[arAttr objectAtIndex:i] stringValue] forKey:[[arAttr objectAtIndex:i] name]];
                    }
                }
            }


            // PROCESS FOR READ CHILD NODE IN CURRENT NODE -------------------------

            for (CXMLNode *childNode in [itemNode children])
            {
                //   NSLog(@"count -- %d ---  %@",[childNode childCount],[childNode children]);

                // IF ANY CHILD NODE HAVE MULTIPLE CHILDRENS THEN DO THIS....- 
                if ([childNode childCount] > 1)
                {
                    NSMutableDictionary *dicChild = [[NSMutableDictionary alloc] init];
                    for (CXMLNode *ChildItemNode in [childNode children])
                    {
                        [dicChild setValue:[ChildItemNode stringValue] forKey:[ChildItemNode name]];
                    }
                     [dic setValue:dicChild forKey:[childNode name]];
                }else
                {
                    [dic setValue:[childNode stringValue] forKey:[childNode name]];
                }

            }

            [arrReturn addObject:dic];
        }

        //    NSLog(@"%@",arrReturn);
        return arrReturn;
    }


may this help for multiple get child 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top