2013-10-11 2 views
0

Я новичок в xml и хочу проанализировать его с помощью touch xml.parse xml using touch xml

<?xml version="1.0" standalone="yes"?> 
<DataSetMenu xmlns="http://tempuri.org/DataSetMenu.xsd"> 
    <MenuCategories> 
    <MenuCatID>10108</MenuCatID> 
    <MenuCatName>SPEICALS</MenuCatName> 
    <BusinessEntityID>20137</BusinessEntityID> 
    <DisplayIndex>0</DisplayIndex> 
    <MenuCatDesc /> 
    <Visible>true</Visible> 
    <ImagePath /> 
    </MenuCategories> 
    <MenuCategories> 
    <MenuCatID>10109</MenuCatID> 
    <MenuCatName>GENERAL MENU</MenuCatName> 
    <BusinessEntityID>20137</BusinessEntityID> 
    <DisplayIndex>1</DisplayIndex> 
    <MenuCatDesc /> 
    <Visible>true</Visible> 
    <ImagePath /> 
    </MenuCategories> 
    <MenuGroups> 

    <MenuGroupID>110079</MenuGroupID> 
    <MenuCatID>10108</MenuCatID> 
    <MenuGroupName>PIZZA&amp;WINGS&amp;DRINK DEAL</MenuGroupName> 
    <MenuGroupDesc /> 
    <Visible>true</Visible> 
    <DisplayIndex>0</DisplayIndex> 
    <MenuTypeID>1</MenuTypeID> 
    <ImagePath /> 
    <ServiceTimeEnforced>false</ServiceTimeEnforced> 
    <ServiceStartTime>2013-04-15T11:00:00-05:00</ServiceStartTime> 
    <ServiceEndTime>2013-04-15T15:00:00-05:00</ServiceEndTime> 
    <Monday>true</Monday> 
    <Tuesday>true</Tuesday> 
    <Wednesday>true</Wednesday> 
    <Thursday>true</Thursday> 
    <Friday>true</Friday> 
    <Saturday>true</Saturday> 
    <Sunday>true</Sunday> 
    </MenuGroups> 
    <MenuGroups> 
    <MenuGroupID>110081</MenuGroupID> 
    <MenuCatID>10109</MenuCatID> 
    <MenuGroupName>BUILD YOUR OWN PIZZA</MenuGroupName> 
    <MenuGroupDesc /> 
    <Visible>true</Visible> 
    <DisplayIndex>0</DisplayIndex> 
    <MenuTypeID>1</MenuTypeID> 
    <ImagePath /> 
    <ServiceTimeEnforced>false</ServiceTimeEnforced> 
    <ServiceStartTime>1900-01-01T11:00:00-06:00</ServiceStartTime> 
    <ServiceEndTime>1900-01-01T15:00:00-06:00</ServiceEndTime> 
    <Monday>true</Monday> 
    <Tuesday>true</Tuesday> 
    <Wednesday>true</Wednesday> 
    <Thursday>true</Thursday> 
    <Friday>true</Friday> 
    <Saturday>true</Saturday> 
    <Sunday>true</Sunday> 
    </MenuGroups> 

я пытаюсь использовать этот код

// we will put parsed data in an a array 
    NSMutableArray *res = [[NSMutableArray alloc] init]; 

    // using local resource file 
    NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"20137_ZZ Pizza & Kabob_2013_04_19 01_20_22_Local.xml"]; 
    NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath]; 
    CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease]; 

    NSArray *nodes = NULL; 
    // searching for piglet nodes 
    nodes = [doc nodesForXPath:@"//MenuCatID" error:nil]; 

    for (CXMLElement *node in nodes) { 
     NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; 
     int counter; 
     for(counter = 0; counter < [node childCount]; counter++) { 
      // common procedure: dictionary with keys/values from XML node 
      [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]]; 
     } 

     // and here it is - attributeForName! Simple as that. 
     [item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"]; // <------ this magical arrow is pointing to the area of interest 

     [res addObject:item]; 
     [item release]; 
    } 

    // and we print our results 
    NSLog(@"%@", res); 
    [res release]; 
+0

Где находится проблема/проблема, с которой вы столкнулись? – Tirth

+0

проблема в том, что он влезет в петлю – NullData

ответ

1

На мой взгляд, KissXML лучше для вас https://github.com/robbiehanson/KissXML

Вот пример того, как разобрать ваш файл XML

// your file name 
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"xml"]; 

// load file content into string 
NSString *xmlContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 

// create XMLDocument object 
DDXMLDocument *xml = [[DDXMLDocument alloc] initWithXMLString:xmlContent options:0 error:nil]; 

// Get root element - DataSetMenu for your XMLfile 
DDXMLElement *root = [xml rootElement]; 

// go through all elements in root element (DataSetMenu element) 
for (DDXMLElement *DataSetMenuElement in [root children]) { 
    // if the element name's is MenuCategories then do something 
    if ([[DataSetMenuElement name] isEqualToString:@"MenuCategories"]) { 
     // get MenuCatID 
     NSInteger MenuCatID = [[[DataSetmenuElement elementForName:@"MenuCatID"] objectAtIndex:0] integerValue]; 
    } 
} 

Метод elementForName доставит вас NSArray всех элементов с определенным именем (если вы хотите сначала objectAtIndex: 0), и после этого вы должны иметь определенный тип (integerValue, stringValue, doubleValue)

Надеюсь, это поможет.

+0

Я хочу использовать touchXml, если вы можете мне помочь? – NullData

+0

Вы ищете учебник -> [Parsin XML с TouchXML] (http://foobarpig.com/iphone/parsing-xml-element-attributes-with-touchxml.html) –