0

I have an xml file which i create myself.

   <?xml version='1.0' encoding='utf-8' ?>
    <Root>
        <ChildElements>
            <type name='xx' attr='false'>
                <child id='1' regex='^[0-9]{11}$'>
                    <BBB>
                        <node1 name='aaa' regex='\w{5}'/>
                        <node2 name='bbb' regex='\w{3}'/>
                    </BBB>
                </child>
                <child>
                .
                .
                .
               </child>
           </type>
        </ChildElements>
    </Root>

Something like this. I want to parse it to xml document object and iterate through its nodes, I'm using libxml2 for ios.

 NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"xmlcontent" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSString* xml = [[NSString alloc ] initWithData:xmlData encoding:NSUTF8StringEncoding];
int size=[xml lengthOfBytesUsingEncoding:NSUTF8StringEncoding];    
xmlDocPtr docptr=xmlParseMemory([xml UTF8String], size);

but in docptr object there is only one node, which is Root node, and when I try to get Root node child nodes it returns an empty node which name is @"text".
NSXMLParser easily parses the same XML, but I don't know how to create a document and iterate through nodes by using NSXMLParser.

taffarel
  • 4,015
  • 4
  • 33
  • 61

1 Answers1

0

maybe the error is, that your XML isn't valide:

<?xml version='1.0' encoding='utf-8' ?>
<Root>
    <ChildElements>
        <type name='xx' attr='false' />
            <child id='1' regex='^[0-9]{11}$'>
                <BBB>
                    <node1 name='aaa' regex='w{5}' />
                    <node2 name='bbb' regex='w{3}' />
                </BBB>
            </child>

    </ChildElements>
</Root>

you can validate your XML with an online XML Validator

CarlJ
  • 9,461
  • 3
  • 33
  • 47